单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:4513|回复:1

单片机数控云台master 仿真及源程序

[复制链接]
ID:137190发表于 2016-10-9 21:35|显示全部楼层
0.png

0.png


仿真工程文件及所有完整程序等资料下载地址:
http://www.51hei.com/bbs/dpj-56304-1.html

源程序:
  1. /*********************************************
  2. This program was produced by the
  3. CodeWizardAVR V1.23.8c Standard

  4. Project : SKYT
  5. Version : 0.01
  6. Date   : 2006-12-6
  7. Author  : hlchen                  
  8. Company :                      
  9. Comments:  
  10. 数控云台
  11. Chip type         : ATmega8
  12. Program type       : Application
  13. Clock frequency     : 3.690000 MHz
  14. Memory model       : Small
  15. External SRAM size  : 0
  16. Data Stack size     : 256
  17. *********************************************/
  18. typedef unsigned char uchar;  
  19. typedef unsigned int uint;
  20. #include< mega8.h>  
  21. #include< math.h>  
  22. #include "macro.h"
  23. #include "LCD.h"
  24. //#include "sed1565_s.h"
  25. #define RXB8 1
  26. #define TXB8 0
  27. #define UPE 2
  28. #define OVR 3
  29. #define FE 4
  30. #define UDRE 5
  31. #define RXC 7
  32. #define Light 5
  33. #define FRAMING_ERROR (1<<FE)
  34. #define PARITY_ERROR (1<<UPE)
  35. #define DATA_OVERRUN (1<<OVR)
  36. #define DATA_REGISTER_EMPTY (1<<UDRE)
  37. #define RX_COMPLETE (1<<RXC)
  38. #define sysInitLcd 0
  39. // USART Receiver buffer
  40. #define RX_BUFFER_SIZE 8
  41. char rx_buffer[RX_BUFFER_SIZE];
  42. unsigned char rx_wr_index,rx_rd_index,rx_counter;
  43. // This flag is set on USART Receiver buffer overflow
  44. bit rx_buffer_overflow,b_ComEnable;
  45. uchar uc_syssta;
  46. // USART Receiver interrupt service routine
  47. #pragma savereg-
  48. interrupt [USART_RXC] void uart_rx_isr(void)
  49. {
  50. char status,data;
  51. #asm
  52.    push r26
  53.    push r27
  54.    push r30
  55.    push r31
  56.    in   r26,sreg
  57.    push r26
  58. #endasm
  59. status=UCSRA;
  60. data=UDR;

  61. if ((status& (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
  62.    {
  63.    rx_buffer[rx_wr_index]=data;
  64.    if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
  65.    if (++rx_counter == RX_BUFFER_SIZE)
  66.      {
  67.      rx_counter=0;
  68.      rx_buffer_overflow=1;
  69.      };
  70.    };
  71. if ((rx_buffer[0]=='o')&&(rx_buffer[1]=='k')) b_ComEnable=1;
  72. #asm
  73.    pop  r26
  74.    out  sreg,r26
  75.    pop  r31
  76.    pop  r30
  77.    pop  r27
  78.    pop  r26
  79. #endasm
  80. }
  81. #pragma savereg+

  82. #ifndef _DEBUG_TERMINAL_IO_
  83. // Get a character from the USART Receiver buffer
  84. #define _ALTERNATE_GETCHAR_
  85. #pragma used+
  86. char getchar(void)
  87. {
  88. char data;
  89. while (rx_counter==0);
  90. data=rx_buffer[rx_rd_index];
  91. if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
  92. #asm("cli")
  93. --rx_counter;
  94. #asm("sei")
  95. return data;
  96. }
  97. #pragma used-
  98. #endif

  99. // USART Transmitter buffer
  100. #define TX_BUFFER_SIZE 8
  101. char tx_buffer[TX_BUFFER_SIZE];
  102. unsigned char tx_wr_index,tx_rd_index,tx_counter;

  103. // USART Transmitter interrupt service routine
  104. #pragma savereg-
  105. interrupt [USART_TXC] void uart_tx_isr(void)
  106. {
  107. #asm
  108.    push r26
  109.    push r27
  110.    push r30
  111.    push r31
  112.    in   r26,sreg
  113.    push r26
  114. #endasm
  115. if (tx_counter)
  116.    {
  117.    --tx_counter;
  118.    UDR=tx_buffer[tx_rd_index];
  119.    if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
  120.    };

  121. #asm
  122.    pop  r26
  123.    out  sreg,r26
  124.    pop  r31
  125.    pop  r30
  126.    pop  r27
  127.    pop  r26
  128. #endasm
  129. }
  130. #pragma savereg+

  131. #ifndef _DEBUG_TERMINAL_IO_
  132. // Write a character to the USART Transmitter buffer
  133. #define _ALTERNATE_PUTCHAR_
  134. #pragma used+
  135. void putchar(char c)
  136. {
  137. while (tx_counter == TX_BUFFER_SIZE);
  138. #asm("cli")
  139. if (tx_counter || ((UCSRA& DATA_REGISTER_EMPTY)==0))
  140.    {
  141.    tx_buffer[tx_wr_index]=c;
  142.    if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
  143.    ++tx_counter;
  144.    }
  145. else UDR=c;
  146. #asm("sei")
  147. }
  148. #pragma used-
  149. #endif
  150. // Standard Input/Output functions
  151. #include< stdio.h>  
  152. uchar ucInputValue;
  153. uint uiLastADValue[3];
  154. uchar ucInputParam0;
  155. uchar ucChannelN,uc_ReceiveSta;
  156. bit bInputChange,bMotorSpeed,bLight,bReceiveOpend;      
  157. void inputdeal(void);
  158. void TransmitEnable(void);
  159. void ReceiveEnable(void);
  160. void Communication(void);
  161. void LcdShow(void);
  162. #define HighSpeed 0x33
  163. #define SlowSpeed 0x99
  164. #define ADC_VREF_TYPE 0x00    
  165. #define RE 0
  166. #define DE 1
  167. // ADC interrupt service routine
  168. interrupt [TIM0_OVF] void timer0_ovf_isr(void)
  169. {
  170. // Place your code here
  171. TCNT0=0xf0;
  172. }
  173. interrupt [ADC_INT] void adc_isr(void)
  174. {
  175. uint adc_data;
  176. // Read the AD conversion result
  177. adc_data=ADCW;

  178. // Place your code here
  179. switch (ucChannelN)
  180. {
  181. case 0:
  182.                  if (uiLastADValue[0]!=adc_data)
  183.              {
  184.              uiLastADValue[0]=adc_data;
  185.              ucInputParam0=uiLastADValue[0]*24/1024;
  186.            //ucInputParam0 = tmp/100;
  187.            //ucInputParam1 = (tmp%100)/10;
  188.            //ucInputParam2 = (tmp%100)%10;
  189.            bInputChange=1;
  190.            ucInputValue=0x01;
  191.           
  192.            }
  193.            ADMUX=0x01;
  194.            break;  
  195. case 1:
  196.            if (uiLastADValue[1]!=adc_data)
  197.              {
  198.              uiLastADValue[1]=adc_data;
  199.              ucInputParam0=uiLastADValue[1]*12/1024;
  200.            //ucInputParam0 = tmp/100;
  201.            //ucInputParam1 = (tmp%100)/10;
  202.            //ucInputParam2 = (tmp%100)%10;
  203.            bInputChange=1;
  204.            ucInputValue=0x02;
  205.         
  206.            }
  207.            ADMUX=0x02;
  208.            break;  
  209. case 2:
  210.            if (uiLastADValue[2]!=adc_data)
  211.              {
  212.              uiLastADValue[2]=adc_data;
  213.              //ucInputParam0=uiLastADValue[2]*6/1024;
  214.                  if (abs(uiLastADValue[2]-1023)<100)
  215.                        ucInputValue=3;
  216.              else if (abs(uiLastADValue[2]-465)<50)
  217.                        ucInputValue=4;
  218.                  else if (abs(uiLastADValue[2]-292)<25)
  219.                        ucInputValue=5;      
  220.                  else if (abs(uiLastADValue[2]-204)<25)
  221.                        ucInputValue=6;
  222.                  else if (abs(uiLastADValue[2]-146)<12)
  223.                        ucInputValue=7;
  224.                  else if (abs(uiLastADValue[2]-93)<12)
  225.                        ucInputValue=8;
  226.                  else if (abs(uiLastADValue[2])<6)
  227.                        ucInputValue=9;
  228.                  else break;
  229.            //       else if (abs(uiLastADValue[2]-512
  230.            //ucInputParam0 = tmp/100;
  231.            //ucInputParam1 = (tmp%100)/10;
  232.            //ucInputParam2 = (tmp%100)%10;
  233.            bInputChange=1;
  234. //           ucInputValue=ucInputParam0+2;
  235.         
  236.            }
  237.            ADMUX=0x00;
  238.            break;          
  239. default:
  240.            break;
  241. }  
  242. ADCSRA|=0x40;
  243. //ADMUX^=0x01;  
  244. if (ucChannelN++==3)   ucChannelN=0;

  245. }

  246. // Declare your global variables here

  247. void main(void)
  248. {
  249. // Declare your local variables here

  250. uint i;
  251. // Input/Output Ports initialization
  252. // Port B initialization
  253. // Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out
  254. // State0=0 State1=0 State2=0 State3=0 State4=0 State5=0 State6=0 State7=0
  255. PORTB=0x00;
  256. DDRB=0xFF;

  257. // Port C initialization
  258. // Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out
  259. // State0=0 State1=0 State2=0 State3=0 State4=0 State5=0 State6=0
  260. PORTC=0x00;
  261. DDRC=0xff;

  262. // Port D initialization
  263. // Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=Out Func7=Out
  264. // State0=0 State1=0 State2=0 State3=0 State4=0 State5=0 State6=0 State7=0
  265. PORTD=0x00;
  266. DDRD=0x73;

  267. // Timer/Counter 0 initialization
  268. // Clock source: System Clock
  269. // Clock value: 3690.000 kHz
  270. TCCR0=0x03;
  271. TCNT0=0xf0;


  272. // Timer/Counter 1 initialization
  273. // Clock source: System Clock
  274. // Clock value: Timer 1 Stopped
  275. // Mode: Normal top=FFFFh
  276. // OC1A output: Discon.
  277. // OC1B output: Discon.
  278. // Noise Canceler: Off
  279. // Input Capture on Falling Edge
  280. //模式1(计数器上限值=0xff) PWM频率 = 系统时钟频率/(分频系数*2*计数器上限值))
  281. TCCR1A=0x00;
  282. //COM1A1,COM1A0,COM1B1,COM1B0=1表示升序记数时比较匹配将置位OC1A/OC1B,降序记数时比较匹配将清零OC1A/OC1B
  283. //即OCR1AL<tcnt1<TOP(0xff)时 OC1A为高 tcnt1<OCR1AL时OC1A为低
  284. //WGM11=0 WGM10=1
  285. TCCR1B=0x00;
  286. //WGM13=0 WGM12=0 CS12=1 CS11=0 CS10=0 分频系数=256
  287. TCNT1H=0x00;
  288. TCNT1L=0x00;
  289. OCR1AH=0x00;
  290. OCR1AL=0x00; //占空比=40% (0xff-0x99)/0xff=0.4
  291.            //占空比=80% 0x33
  292. OCR1BH=0x00;
  293. OCR1BL=0x00;

  294. // Timer/Counter 2 initialization
  295. // Clock source: System Clock
  296. // Clock value: Timer 2 Stopped
  297. // Mode: Normal top=FFh
  298. // OC2 output: Disconnected
  299. ASSR=0x00;
  300. TCCR2=0x00;
  301. TCNT2=0x00;
  302. OCR2=0x00;

  303. // External Interrupt(s) initialization
  304. // INT0: Off
  305. // INT1: Off
  306. GICR|=0x00;
  307. MCUCR=0x00;

  308. // Timer(s)/Counter(s) Interrupt(s) initialization
  309. TIMSK=0X01;

  310. // USART initialization
  311. // Communication Parameters: 8 Data, 1 Stop, No Parity
  312. // USART Receiver: On
  313. // USART Transmitter: On
  314. // USART Mode: Asynchronous
  315. // USART Baud rate: 1200

  316. UCSRA=0x00;
  317. UCSRB=0xD8;
  318. UCSRC=0x86;
  319. UBRRH=0x00;
  320. UBRRL=0x33;
  321. // Analog Comparator initialization
  322. // Analog Comparator: Off
  323. // Analog Comparator Input Capture by Timer/Counter 1: Off
  324. // Analog Comparator Output: Off
  325. ACSR=0x80;
  326. SFIOR=0x00;

  327. // ADC initialization
  328. // ADC Clock frequency: 115.313 kHz
  329. // ADC Voltage Reference: AREF pin
  330. // ADC High Speed Mode: Off
  331. // ADC Auto Trigger Source: Timer0 Overflow
  332. ADMUX=0x00;
  333. ADCSRA=0x00;
  334. //SFIOR&=0x0F;
  335. //SFIOR|=0x80;

  336. // Global enable interrupts  

  337. InitLCD();







  338. #asm("sei")
  339. TransmitEnable();
  340. printf("Ready?");
  341. for (i=0;i<10000;i++);
  342. while (1)
  343.      {
  344.      // Place your code here
  345.      if (tx_counter==0) ReceiveEnable();  

  346.      if (b_ComEnable)
  347.        {
  348.            b_ComEnable=0;
  349.            LcdShow();
  350.            break;
  351.          }
  352.      };
  353. while(1)
  354. {

  355.      //inputdeal();
  356. }
  357. }
  358. void LcdShow(void)
  359. {
  360.        Show_Circle();
  361.        Show_HalfCircle();
  362.        Show_Preset();
  363.        Show_Slow();
  364. }
  365. void TransmitEnable(void)
  366. {
  367. PORTB|=Bit(RE);
  368. PORTB|=Bit(DE);
  369. }
  370. void ReceiveEnable(void)
  371. {

  372. PORTB&=Bit(RE);
  373. PORTB&=Bit(DE);

  374. }
  375. void TransmitCommandOk(void)
  376. {
  377.        //if (uc_syssta==sysInitLcd)
  378.        //       LcdShow();      
  379. }
  380. void Communication(void)
  381. {
  382.       
  383.      switch (uc_ReceiveSta)
  384.      {
  385.      case 0:
  386.                if (getchar()=='R')
  387.                      uc_ReceiveSta++;
  388.            else if (getchar()=='O')
  389.                  uc_ReceiveSta++;
  390.                 
  391.            break;
  392.      case 1:
  393.                if (getchar()=='e')
  394.                  uc_ReceiveSta++;
  395.            else if (getchar()=='k')
  396.                  {
  397.                  uc_ReceiveSta=0;
  398.                  b_ComEnable=1;
  399.                  }
  400.            else
  401.                  uc_ReceiveSta=0;
  402.            break;
  403.      case 2:
  404.                if (getchar()=='a')
  405.                  uc_ReceiveSta++;
  406.            else
  407.                      uc_ReceiveSta=0;
  408.            break;
  409.      case 3:
  410.                if (getchar()=='d')
  411.                  uc_ReceiveSta++;
  412.            else
  413.                  uc_ReceiveSta=0;
  414.                  break;  
  415.      case 4:
  416.                uc_ReceiveSta=0;
  417.                if (getchar()=='y')
  418.                  printf("ok");
  419.            break;
  420.      }              
  421.     
  422.     
  423. }
  424. void inputdeal(void)
  425. {
  426.        if (!bInputChange) return;
  427.        bInputChange=0;
  428.        switch (ucInputValue)
  429.        {
  430.        case 0x01:
  431.            //printf("%bu,%bu,%bu\n",ucInputParam0,ucInputParam1,ucInputParam2);
  432.            //       PaintDegree(ucInputParam0,0);  
  433.       
  434.            break;
  435.        case 0x02:
  436.            //       PaintDegree(ucInputParam0,1);
  437.       
  438.            break;
  439.        case 0x03://key1
  440.                if (!bLight) PORTB|=Bit(Light);
  441.                else PORTB&=~Bit(Light);
  442.                bLight=!bLight;
  443.            PORTD|=0x04;
  444.            break;
  445.        case 0x04://key2
  446.            //       PORTD&=0xc3;
  447.            PORTD|=0x08;  
  448.            //       if (bMotorSpeed)     {Show_Slow();OCR1AL=SlowSpeed;}
  449.          //       else   {Show_Quick();OCR1AL=HighSpeed;}
  450.            bMotorSpeed=!bMotorSpeed;
  451.           
  452.            break;  
  453.        case 0x05://key3
  454.            //       PORTD&=0xc3;
  455.            PORTD|=0x0c;
  456.            break;
  457.        case 0x06://key4
  458.            //       PORTD&=0xc3;
  459.            PORTD|=0x10;
  460.            break;
  461.        case 0x07://key5
  462.            //       PORTD&=0xc3;
  463.            PORTD|=0x14;
  464.            break;
  465.        case 0x08://key6  
  466.            //PORTD&=0xc3;
  467.            PORTD|=0x18;
  468.            break;
  469.        case 0x09://keyrelease
  470.            PORTD&=0xc3;  
  471.            break;
  472.        default:
  473.            break;
  474.        }
  475. }
复制代码

ID:1092949发表于 2023-9-8 09:40|显示全部楼层
可否更详细说明一下,有些看不懂

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网