找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1315|回复: 0
收起左侧

atmega128单片机双串口使用

[复制链接]
ID:663772 发表于 2019-12-14 15:14 | 显示全部楼层 |阅读模式
  1. /ICC-AVR application builder
  2. // Target : M128
  3. // Crystal: 3.6864Mhz
  4. #include
  5. #include
  6. //定义相关的数据变量
  7. unsigned int DATA_NUM1,DATA_NUM2;       //定义接收的数据个数
  8. unsigned char BIT_SEND;       //定义发送标志位 =0 表示需要发送 =1 表示不需要发送
  9. unsigned char DATA1[400];
  10. unsigned char DATA2[330];
  11. void port_init(void)
  12. {
  13. PORTA = 0x00;
  14. DDRA  = 0x00;
  15. PORTB = 0x38;
  16. DDRB  = 0x38;
  17. PORTC = 0x00; //m103 output only
  18. DDRC  = 0x00;
  19. PORTD = 0x00;
  20. DDRD  = 0x00;
  21. PORTE = 0x00;
  22. DDRE  = 0x00;
  23. PORTF = 0x00;
  24. DDRF  = 0x00;
  25. PORTG = 0x00;
  26. DDRG  = 0x00;
  27. }
  28. //UART0 initialize
  29. // desired baud rate: 9600
  30. // actual: baud rate:9600 (0.0%)
  31. // char size: 8 bit
  32. // parity: Disabled
  33. void uart0_init(void)
  34. {
  35. UCSR0B = 0x00; //disable while setting baud rate
  36. UCSR0A = 0x00;
  37. UCSR0C = 0x06;
  38. UBRR0L = 0x17; //set baud rate lo
  39. UBRR0H = 0x00; //set baud rate hi
  40. UCSR0B = 0xF8;
  41. }
  42. #pragma interrupt_handler uart0_rx_isr:19
  43. void uart0_rx_isr(void)
  44. {
  45. //uart has received a character in UDR
  46. unsigned char i,j;;
  47. DATA1[DATA_NUM1]=UDR0;
  48. DATA_NUM1+=1;
  49. }
  50. #pragma interrupt_handler uart0_udre_isr:20
  51. void uart0_udre_isr(void)
  52. {
  53. //character transferred to shift register so UDR is now empty
  54. }
  55. #pragma interrupt_handler uart0_tx_isr:21
  56. void uart0_tx_isr(void)
  57. {
  58. //character has been transmitted
  59. }
  60. //UART1 initialize
  61. // desired baud rate:4800
  62. // actual baud rate:4800 (0.0%)
  63. // char size: 7 bit
  64. // parity: Disabled
  65. //void uart1_init(void)
  66. //{
  67. // UCSR1B = 0x00; //disable while setting baud rate
  68. // UCSR1A = 0x00;
  69. // UCSR1C = 0x0C;
  70. // UBRR1L = 0x2F; //set baud rate lo
  71. // UBRR1H = 0x00; //set baud rate hi
  72. // UCSR1B = 0xF8;
  73. //}
  74. //UART1 initialize
  75. // desired baud rate:4800
  76. // actual baud rate:4800 (0.0%)
  77. // char size: 7 bit
  78. // parity: Even
  79. /*
  80. void uart1_init(void)
  81. {
  82. UCSR1B = 0x00; //disable while setting baud rate
  83. UCSR1A = 0x00;
  84. UCSR1C = 0x2C;
  85. UBRR1L = 0x5F; //set baud rate lo
  86. UBRR1H = 0x00; //set baud rate hi
  87. UCSR1B = 0x68;
  88. }
  89. */


  90. //UART1 initialize
  91. // desired baud rate:4800
  92. // actual baud rate:4800 (0.0%)
  93. // char size: 7 bit
  94. // parity: Even
  95. void uart1_init(void)
  96. {
  97. UCSR1B = 0x00; //disable while setting baud rate
  98. UCSR1A = 0x00;
  99. UCSR1C = 0x2C;
  100. UBRR1L = 0x2F; //set baud rate lo
  101. UBRR1H = 0x00; //set baud rate hi
  102. UCSR1B = 0xf8;
  103. }

  104. #pragma interrupt_handler uart1_rx_isr:31
  105. void uart1_rx_isr(void)
  106. {
  107. //uart has received a character in UDR
  108. }
  109. #pragma interrupt_handler uart1_udre_isr:32
  110. void uart1_udre_isr(void)
  111. {
  112. //character transferred to shift register so UDR is now empty
  113. }
  114. #pragma interrupt_handler uart1_tx_isr:33
  115. void uart1_tx_isr(void)
  116. {
  117. //character has been transmitted
  118. }
  119. //call this routine to initialize all peripherals
  120. void init_devices(void)
  121. {
  122. //stop errant interrupts until set up
  123. CLI(); //disable all interrupts
  124. XDIV  = 0x00; //xtal divider
  125. XMCRA = 0x00; //external memory
  126. port_init();
  127. uart0_init();
  128. uart1_init();
  129. MCUCR = 0x00;
  130. EICRA = 0x00; //extended ext ints
  131. EICRB = 0x00; //extended ext ints
  132. EIMSK = 0x00;
  133. TIMSK = 0x00; //timer interrupt sources
  134. ETIMSK = 0x00; //extended timer interrupt sources
  135. SEI(); //re-enable interrupts
  136. //all peripherals are now initialized
  137. }
  138. //***************************************
  139. //发送数据
  140. //***************************************
  141. void USART0_Transmit(unsigned char data)
  142. {
  143. //等待发送缓冲器为空
  144. while(!(UCSR0A&(1<<udre0)));
  145.   UDR0=data;
  146. }
  147. void USART1_Transmit(unsigned char data)
  148. {
  149. //等待发送缓冲器为空
  150. while(!(UCSR1A&(1<<udre1)));
  151.   UDR1=data;
  152. }
  153. //**********************************************
  154. //分析数据 出现数据测量失误的情况 就发这个数据
  155. //**********************************************
  156. void DATA_fenxi1()
  157. {
  158.   DATA2[0]=0X7F;    //起始位
  159. DATA2[1]=0X02;
  160. DATA2[2]=0X0D;
  161. DATA2[3]=0X0A;

  162. }</udre1)));
  163. </udre0)));
  164. //*************************************
  165. //分析完整的数据
  166. //*************************************
  167. void DATA_fenxi()
  168. {
  169. DATA2[0]=0X7F;    //起始位
  170. DATA2[1]=0X02;
  171. DATA2[2]=0X0D;
  172. DATA2[3]=0X0A;

  173. }
  174. //*************************************
  175. //延时子程序 延时0.5s
  176. //*************************************
  177. void DEL_05S()
  178. {
  179. unsigned char i,j;
  180. for(i=0;i<250;i++)
  181.   {
  182.    for(j=0;j<200;j++)
  183.    {;}
  184.   }
  185. }
  186. //主程序 用来控制相关的协议变化
  187. void main()
  188. {
  189. unsigned int i;
  190. DATA_NUM1=0X00;
  191. DATA_NUM2=0x00;
  192. init_devices();
  193. ///////////////////////
  194. //for(i=0;i<315;i++)
  195. // {
  196. // DATA1[i]=0x32;
  197. // }
  198. // DATA_fenxi();
  199. ///////////////////////
  200. START:
  201. DATA_NUM1=0;
  202. DEL_05S();
  203. DEL_05S();
  204.   PORTB=0B11011110;    //单片机无问题红灯亮
  205. DEL_05S();
  206. DEL_05S();
  207. /////////////////////////////////
  208. //  for(i=0;i<315;i++)
  209. // {
  210. //  PORTB=0B11010110;   //发送数据 红灯和绿灯亮
  211. //  USART1_Transmit(DATA2[i]);
  212. // }
  213. // goto START;

  214. /////////////////////////////////
  215. if(DATA_NUM1>0){
  216. while(DATA_NUM1!=363)
  217. {
  218.   unsigned char i;
  219.   PORTB=0B11001110;  //接受数据状态  红灯亮 黄灯亮
  220.   for(i=0;i<80;i++)
  221.   {
  222.    DEL_05S();
  223.    if(DATA_NUM1>=362)
  224.    {goto FENXI;}
  225.   }
  226.   if(DATA_NUM1>=362)
  227.    {goto FENXI;}
  228.   else
  229.     {
  230.      if(DATA_NUM1<=300)
  231.    {goto FENXI1;}
  232.     }
  233. }
  234. FENXI:
  235. DEL_05S();
  236. DEL_05S();
  237. DATA_NUM1=0x00;
  238. DATA_fenxi();
  239.   //USART1_Transmit(DATA1[0]);
  240. for(i=0;i<315;i++)
  241. {
  242.   PORTB=0B11010110;   //发送数据 红灯和绿灯亮
  243.   USART1_Transmit(DATA2[i]);
  244. }
  245. goto START;
  246. goto START;
  247. goto START;
  248. FENXI1:
  249. DATA_NUM1=0X00;
  250. DATA_fenxi1();
  251. for(i=0;i<315;i++)
  252. {
  253.   PORTB=0B11010110;    //发送数据 红灯和绿灯亮
  254.   USART1_Transmit(DATA2[i]);
  255. }
  256. goto START;
  257. goto START;
  258. goto START;
  259. }
  260. goto START;
  261. }
复制代码


atmega 128 双串口使用.docx

17.63 KB, 下载次数: 7, 下载积分: 黑币 -5

atmega 128 双串口使用

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

快速回复 返回顶部 返回列表