samir82show

Untitled

Feb 1st, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <mega32.h>
  2.  
  3. #ifndef RXB8
  4. #define RXB8 1
  5. #endif
  6.  
  7. #ifndef TXB8
  8. #define TXB8 0
  9. #endif
  10.  
  11. #ifndef UPE
  12. #define UPE 2
  13. #endif
  14.  
  15. #ifndef DOR
  16. #define DOR 3
  17. #endif
  18.  
  19. #ifndef FE
  20. #define FE 4
  21. #endif
  22.  
  23. #ifndef UDRE
  24. #define UDRE 5
  25. #endif
  26.  
  27. #ifndef RXC
  28. #define RXC 7
  29. #endif
  30.  
  31. #define FRAMING_ERROR (1<<FE)
  32. #define PARITY_ERROR (1<<UPE)
  33. #define DATA_OVERRUN (1<<DOR)
  34. #define DATA_REGISTER_EMPTY (1<<UDRE)
  35. #define RX_COMPLETE (1<<RXC)
  36.  
  37. // USART Receiver buffer
  38. #define RX_BUFFER_SIZE 8
  39. char rx_buffer[RX_BUFFER_SIZE];
  40.  
  41. #if RX_BUFFER_SIZE <= 256
  42. unsigned char rx_wr_index,rx_rd_index,rx_counter;
  43. #else
  44. unsigned int rx_wr_index,rx_rd_index,rx_counter;
  45. #endif
  46.  
  47. // This flag is set on USART Receiver buffer overflow
  48. bit rx_buffer_overflow;
  49.  
  50. // USART Receiver interrupt service routine
  51. interrupt [USART_RXC] void usart_rx_isr(void)
  52. {
  53. char status,data;
  54. status=UCSRA;
  55. data=UDR;
  56. if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
  57. {
  58. rx_buffer[rx_wr_index++]=data;
  59. #if RX_BUFFER_SIZE == 256
  60. // special case for receiver buffer size=256
  61. if (++rx_counter == 0)
  62. {
  63. #else
  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. #endif
  69. rx_buffer_overflow=1;
  70. }
  71. }
  72. }
  73.  
  74. #ifndef _DEBUG_TERMINAL_IO_
  75. // Get a character from the USART Receiver buffer
  76. #define _ALTERNATE_GETCHAR_
  77. #pragma used+
  78. char getchar(void)
  79. {
  80. char data;
  81. while (rx_counter==0);
  82. data=rx_buffer[rx_rd_index++];
  83. #if RX_BUFFER_SIZE != 256
  84. if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
  85. #endif
  86. #asm("cli")
  87. --rx_counter;
  88. #asm("sei")
  89. return data;
  90. }
  91. #pragma used-
  92. #endif
  93.  
  94. // USART Transmitter buffer
  95. #define TX_BUFFER_SIZE 8
  96. char tx_buffer[TX_BUFFER_SIZE];
  97.  
  98. #if TX_BUFFER_SIZE <= 256
  99. unsigned char tx_wr_index,tx_rd_index,tx_counter;
  100. #else
  101. unsigned int tx_wr_index,tx_rd_index,tx_counter;
  102. #endif
  103.  
  104. // USART Transmitter interrupt service routine
  105. interrupt [USART_TXC] void usart_tx_isr(void)
  106. {
  107. if (tx_counter)
  108. {
  109. --tx_counter;
  110. UDR=tx_buffer[tx_rd_index++];
  111. #if TX_BUFFER_SIZE != 256
  112. if (tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
  113. #endif
  114. }
  115. }
  116.  
  117. #ifndef _DEBUG_TERMINAL_IO_
  118. // Write a character to the USART Transmitter buffer
  119. #define _ALTERNATE_PUTCHAR_
  120. #pragma used+
  121. void putchar(char c)
  122. {
  123. while (tx_counter == TX_BUFFER_SIZE);
  124. #asm("cli")
  125. if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
  126. {
  127. tx_buffer[tx_wr_index++]=c;
  128. #if TX_BUFFER_SIZE != 256
  129. if (tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
  130. #endif
  131. ++tx_counter;
  132. }
  133. else
  134. UDR=c;
  135. #asm("sei")
  136. }
  137. #pragma used-
  138. #endif
  139.  
  140. // Standard Input/Output functions
  141. #include <stdio.h>
  142.  
  143. // Declare your global variables here
  144.  
  145. void main(void)
  146. {
  147. // Declare your local variables here
  148. bit state = 0;
  149. // Input/Output Ports initialization
  150. // Port A initialization
  151. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  152. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  153. PORTA=0x00;
  154. DDRA=0x00;
  155.  
  156. // Port B initialization
  157. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  158. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  159. PORTB=0x00;
  160. DDRB=0x00;
  161.  
  162. // Port C initialization
  163. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  164. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  165. PORTC=0x02;
  166. DDRC=0x01;
  167.  
  168. // Port D initialization
  169. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  170. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  171. PORTD=0x00;
  172. DDRD=0x00;
  173.  
  174. // Timer/Counter 0 initialization
  175. // Clock source: System Clock
  176. // Clock value: Timer 0 Stopped
  177. // Mode: Normal top=0xFF
  178. // OC0 output: Disconnected
  179. TCCR0=0x00;
  180. TCNT0=0x00;
  181. OCR0=0x00;
  182.  
  183. // Timer/Counter 1 initialization
  184. // Clock source: System Clock
  185. // Clock value: Timer1 Stopped
  186. // Mode: Normal top=0xFFFF
  187. // OC1A output: Discon.
  188. // OC1B output: Discon.
  189. // Noise Canceler: Off
  190. // Input Capture on Falling Edge
  191. // Timer1 Overflow Interrupt: Off
  192. // Input Capture Interrupt: Off
  193. // Compare A Match Interrupt: Off
  194. // Compare B Match Interrupt: Off
  195. TCCR1A=0x00;
  196. TCCR1B=0x00;
  197. TCNT1H=0x00;
  198. TCNT1L=0x00;
  199. ICR1H=0x00;
  200. ICR1L=0x00;
  201. OCR1AH=0x00;
  202. OCR1AL=0x00;
  203. OCR1BH=0x00;
  204. OCR1BL=0x00;
  205.  
  206. // Timer/Counter 2 initialization
  207. // Clock source: System Clock
  208. // Clock value: Timer2 Stopped
  209. // Mode: Normal top=0xFF
  210. // OC2 output: Disconnected
  211. ASSR=0x00;
  212. TCCR2=0x00;
  213. TCNT2=0x00;
  214. OCR2=0x00;
  215.  
  216. // External Interrupt(s) initialization
  217. // INT0: Off
  218. // INT1: Off
  219. // INT2: Off
  220. MCUCR=0x00;
  221. MCUCSR=0x00;
  222.  
  223. // Timer(s)/Counter(s) Interrupt(s) initialization
  224. TIMSK=0x00;
  225.  
  226. // USART initialization
  227. // Communication Parameters: 8 Data, 1 Stop, No Parity
  228. // USART Receiver: On
  229. // USART Transmitter: On
  230. // USART Mode: Asynchronous
  231. // USART Baud Rate: 2400
  232. UCSRA=0x00;
  233. UCSRB=0xD8;
  234. UCSRC=0x86;
  235. UBRRH=0x00;
  236. UBRRL=0x19;
  237.  
  238. // Analog Comparator initialization
  239. // Analog Comparator: Off
  240. // Analog Comparator Input Capture by Timer/Counter 1: Off
  241. ACSR=0x80;
  242. SFIOR=0x00;
  243.  
  244. // ADC initialization
  245. // ADC disabled
  246. ADCSRA=0x00;
  247.  
  248. // SPI initialization
  249. // SPI disabled
  250. SPCR=0x00;
  251.  
  252. // TWI initialization
  253. // TWI disabled
  254. TWCR=0x00;
  255.  
  256. // Global enable interrupts
  257. #asm("sei")
  258.  
  259. while (1)
  260. {
  261. // Place your code here
  262. if(PINC.1 == 0)
  263. {
  264. if(state == 0)
  265. {
  266. putchar(128);
  267. state = 1;
  268. }
  269. else
  270. state = 0;
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment