Advertisement
Guest User

USART

a guest
Feb 10th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. /* Defines */
  2. #define F_CPU 16000000UL
  3. #define FOSC 16000000 // Clock Speed
  4. #define BAUD 9600
  5. #define MYUBRR FOSC/16/BAUD-1
  6. #define USART_RX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
  7. #define USART_TX_BUFFER_SIZE 128 /* 2,4,8,16,32,64,128 or 256 bytes */
  8. #define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
  9. #define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
  10. #define text "Hello from Arduino!\nThis project contains a variety of different functionalities.\nBy going through the menu you can easily setup and control the system.\n"
  11. /* Preprocessor */
  12. #include <avr/io.h>
  13. #include <avr/interrupt.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <util/delay.h>
  17. /* Function Prototypes */
  18. void ReceiveSystemInstructions( char* Buffer, unsigned int BufferSize );
  19. void USART_Transmit_IO( char data, FILE *stream );
  20. void USART_Init( unsigned int baudrate );
  21. void USART_Transmit( unsigned char data);
  22. void USART_PutString(char* StringPointer);
  23. void menu(int menuposition);
  24. void sendADCValue(void);
  25. void readLightingSensor(void);
  26. /* Buffers */
  27. char USART_ReceiveBuffer[USART_RX_BUFFER_SIZE];
  28. static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
  29. char USART_Instruction[32];
  30. /* Static Variables */
  31. static volatile unsigned char WritingPositionOnTheBuffer;
  32. static volatile unsigned char ReadingPositionOnTheBuffer;
  33. static volatile unsigned char USART_TxHead;
  34. static volatile unsigned char USART_TxTail;
  35. static FILE mystdout = FDEV_SETUP_STREAM(USART_Transmit_IO, NULL, _FDEV_SETUP_WRITE);
  36. /* String Declarations */
  37. char String[] = text;
  38. /* Flags */
  39. volatile uint8_t sendflag = 0;
  40.  
  41. int main(void)
  42. {
  43. /* Outputs */
  44. DDRD |= (1<<PIND7)|(1<<PIND6)|(1<<PIND5)|(1<<PIND4)|(1<<PIND3)|(1<<PIND2);
  45. /* Button ADC conversion start/stop */
  46. DDRB &= ~(1<<PINB0);
  47. /* Pull up resistor */
  48. PORTB |= (1<<PINB0);
  49. /* ADC */
  50. ADCSRA |= 1<<ADPS2;
  51. ADMUX |= 1<<ADLAR;
  52. ADMUX |= 1<<REFS0;
  53. ADCSRA |= 1<<ADIE;
  54. ADCSRA |= 1<<ADEN;
  55. /* Global Interrupts Enabled */
  56. sei();
  57. /* USART */
  58. USART_Init( MYUBRR );
  59. stdout = &mystdout;
  60. USART_PutString(String);
  61. menu(0);
  62. /* Menu Flags */
  63. int pmenu = 0;
  64. int LEDs = 0;
  65. int LEDMenuMessage = 1;
  66. /* Forever Loop */
  67. while(1)
  68. {
  69. ReceiveSystemInstructions(USART_Instruction, USART_TX_BUFFER_SIZE);
  70. if(strcmp(USART_Instruction, "menu") == 0) pmenu = 2;
  71. if(strcmp(USART_Instruction, "ADC") == 0) pmenu = 3;
  72. if(strcmp(USART_Instruction, "LED") == 0) pmenu = 4;
  73. if(strcmp(USART_Instruction, "back") == 0) pmenu = 5;
  74. if(strcmp(USART_Instruction, "LEDs off") == 0) pmenu = 6;
  75. if(strcmp(USART_Instruction, "LED 1 on") == 0) pmenu = 7;
  76. if(strcmp(USART_Instruction, "LED 1 off") == 0) pmenu = 8;
  77. if(strcmp(USART_Instruction, "LED 2 on") == 0) pmenu = 9;
  78. if(strcmp(USART_Instruction, "LED 2 off") == 0) pmenu = 10;
  79. if(strcmp(USART_Instruction, "LED 3 on") == 0) pmenu = 11;
  80. if(strcmp(USART_Instruction, "LED 3 off") == 0) pmenu = 12;
  81. if(strcmp(USART_Instruction, "LED 4 on") == 0) pmenu = 13;
  82. if(strcmp(USART_Instruction, "LED 4 off") == 0) pmenu = 14;
  83. if(strcmp(USART_Instruction, "LED 5 on") == 0) pmenu = 15;
  84. if(strcmp(USART_Instruction, "LED 5 off") == 0) pmenu = 16;
  85. if(strcmp(USART_Instruction, "LED 6 on") == 0) pmenu = 17;
  86. if(strcmp(USART_Instruction, "LED 6 off") == 0) pmenu = 18;
  87. if(strcmp(USART_Instruction, " ") == 0) printf("\n");
  88. if(pmenu > 18) // Needs to be continued
  89. {
  90. printf("Invalid Command\n");
  91. }
  92. switch(pmenu)
  93. {
  94. case 2: menu(0);
  95. pmenu = 0;
  96. break;
  97. case 3: sendADCValue();
  98. pmenu = 0;
  99. break;
  100. case 4: LEDs = 1;
  101. pmenu = 0;
  102. break;
  103. }
  104. if(LEDs)
  105. {
  106. menu(LEDMenuMessage);
  107. LEDMenuMessage = 10; // Clear the flag
  108. switch(pmenu)
  109. {
  110. case 5:
  111. LEDs = 0; // Clear the flag
  112. LEDMenuMessage = 1; // Reset the flag
  113. printf("You are back to the main menu!\n");
  114. menu(0);
  115. break;
  116. case 6:
  117. PORTD &= ~(1<<PIND7);
  118. _delay_ms(200);
  119. PORTD &= ~(1<<PIND6);
  120. _delay_ms(200);
  121. PORTD &= ~(1<<PIND5);
  122. _delay_ms(200);
  123. PORTD &= ~(1<<PIND4);
  124. _delay_ms(200);
  125. PORTD &= ~(1<<PIND3);
  126. _delay_ms(200);
  127. PORTD &= ~(1<<PIND2);
  128. _delay_ms(200);
  129. printf("LED's are off\n");
  130. break;
  131. case 7:
  132. PORTD |= (1<<PIND7);
  133. printf("LED 1 is on\nTo turn it off type: LED 1 off\n");
  134. break;
  135. case 8:
  136. PORTD &= ~(1<<PIND7);
  137. printf("LED 1 is turned off\n");
  138. break;
  139. case 9:
  140. PORTD |= (1<<PIND6);
  141. printf("LED 2 is on\nTo turn it off type: LED 2 off\n");
  142. break;
  143. case 10:
  144. PORTD &= ~(1<<PIND6);
  145. printf("LED 2 is turned off\n");
  146. break;
  147. case 11:
  148. PORTD |= (1<<PIND5);
  149. printf("LED 3 is on\nTo turn it off type: LED 3 off\n");
  150. break;
  151. case 12:
  152. PORTD &= ~(1<<PIND5);
  153. printf("LED 3 is turned off\n");
  154. break;
  155. case 13:
  156. PORTD |= (1<<PIND4);
  157. printf("LED 4 is on\nTo turn it off type: LED 4 off\n");
  158. break;
  159. case 14: {
  160. PORTD &= ~(1<<PIND4);
  161. printf("LED 4 is turned off\n");
  162. break;
  163. case 15:
  164. PORTD |= (1<<PIND3);
  165. printf("LED 5 is on\nTo turn it off type: LED 5 off\n");
  166. break;
  167. case 16:
  168. PORTD &= ~(1<<PIND3);
  169. printf("LED 5 is turned off\n");
  170. break;
  171. case 17:
  172. PORTD |= (1<<PIND2);
  173. printf("LED 6 is on\nTo turn it off type: LED 6 off\n");
  174. break;
  175. case 18:
  176. PORTD &= ~(1<<PIND2);
  177. printf("LED 6 is turned off\n");
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }
  184.  
  185. ISR(ADC_vect)
  186. {
  187. sendflag = 1;
  188. }
  189.  
  190. ISR(USART_RX_vect) // Generating data in the Receive Buffer
  191. {
  192. unsigned char data;
  193. unsigned char tmpWPosition;
  194. /* Read the received data */
  195. data = UDR0;
  196. /* Determine the Write PositionInTheBuffer */
  197. tmpWPosition = (WritingPositionOnTheBuffer + 1) & USART_RX_BUFFER_MASK;
  198. /* Store new index */
  199. WritingPositionOnTheBuffer = tmpWPosition;
  200. /* Store received data in buffer */
  201. USART_ReceiveBuffer[tmpWPosition] = data;
  202. }
  203.  
  204. ISR(USART_UDRE_vect)
  205. {
  206. unsigned char tmptail;
  207.  
  208. /* Check if all data is transmitted */
  209. if ( USART_TxHead != USART_TxTail )
  210. {
  211. /* Calculate buffer index */
  212. tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;
  213. USART_TxTail = tmptail; /* Store new index */
  214.  
  215. UDR0 = USART_TxBuf[tmptail]; /* Start transmission */
  216. }
  217. else
  218. {
  219. UCSR0B &= ~(1<<UDRIE0); /* Disable UDRE interrupt */
  220. }
  221. }
  222.  
  223.  
  224. void USART_Init( unsigned int baudrate )
  225. {
  226. unsigned char x;
  227.  
  228. /* Set the baud rate */
  229. UBRR0H = (unsigned char) (baudrate>>8);
  230. UBRR0L = (unsigned char) baudrate;
  231.  
  232. /* Enable UART receiver and transmitter */
  233. UCSR0B = ( ( 1 << RXCIE0 ) | ( 1 << RXEN0 ) | ( 1 << TXEN0 ) );
  234.  
  235. /* Set frame format: 8 data 1stop */
  236. UCSR0C = (1<<UCSZ01)|(1<<UCSZ00);
  237.  
  238. /* Flush receive buffer */
  239. x = 0;
  240. ReadingPositionOnTheBuffer = x;
  241. WritingPositionOnTheBuffer = x;
  242. USART_TxTail = x;
  243. USART_TxHead = x;
  244. }
  245.  
  246. void USART_Transmit( unsigned char data) //Simple Transmit Function
  247. {
  248. while(!(UCSR0A & (1<<UDRE0))); // Wait
  249. UDR0 = data;
  250. }
  251. void USART_PutString(char* StringPointer)
  252. {
  253. while(*StringPointer != 0x00) //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
  254. {
  255. USART_Transmit(*StringPointer); //Using the simple send function we send one char at a time
  256. StringPointer++; //We increment the pointer so we can read the next char
  257. }
  258. }
  259. void ReceiveSystemInstructions( char* Buffer, unsigned int BufferSize )
  260. {
  261. unsigned char tmpWPosition;
  262. unsigned int i = 0;
  263. int b;
  264. uint8_t ReadPin;
  265.  
  266. while ( WritingPositionOnTheBuffer == ReadingPositionOnTheBuffer ) // Wait for incoming data /
  267. {
  268. for(b = 0; b < 200 ; b++)
  269. {
  270. _delay_ms(1);
  271. ReadPin = ( PINB & ( 1 << PINB0 ) ) ? 1 : 0; // Read the pin
  272. if(!ReadPin)
  273. {
  274. printf("Start Conversion\n");
  275. do
  276. {
  277. ADCSRA |= 1<<ADSC; // Start ADC Conversion
  278. _delay_ms(1000);
  279. ReadPin = ( PINB & ( 1 << PINB0 ) ) ? 1 : 0;
  280. readLightingSensor();
  281. } while (ReadPin == 1);
  282. printf("Stop Conversion\n");
  283. _delay_ms(1000);
  284. }
  285. }
  286. b=0;
  287. }
  288. _delay_ms(10);
  289. for( i=0; i < BufferSize - 1; ++i )
  290. {
  291. tmpWPosition = ( ReadingPositionOnTheBuffer + 1 ) & USART_RX_BUFFER_MASK; // Calculate buffer index /
  292.  
  293. ReadingPositionOnTheBuffer = tmpWPosition; // Store new index /
  294.  
  295. Buffer[i] = USART_ReceiveBuffer[tmpWPosition]; // Return data /
  296. if( WritingPositionOnTheBuffer == ReadingPositionOnTheBuffer )
  297. break;
  298. }
  299. Buffer[i+1] = '\0'; // Adding a Null Terminator
  300. }
  301. void menu(int menuposition)
  302. {
  303. if(menuposition == 0)
  304. {
  305. printf("\nTo access the LEDs type: LED\n");
  306. printf("To get the ADC Value type: ADC\n");
  307. printf("Turning off the LED's by typing: LEDs off\n");
  308. printf("\nTo see the menu again type: menu\n");
  309. }
  310. if(menuposition == 1)
  311. {
  312. printf("\nYou are now in the LED section.\n\nTo turn on the LEDn type LED n on, where n goes from 1 to 6: \n\nGo back by typing: back\n");
  313. }
  314.  
  315. }
  316. void USART_Transmit_IO( char data, FILE *stream )
  317. {
  318. unsigned char tmphead;
  319. /* Calculate buffer index */
  320. tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK;
  321. while ( tmphead == USART_TxTail ); /* Wait for free space in buffer */
  322.  
  323. USART_TxBuf[tmphead] = data; /* Store data in buffer */
  324. USART_TxHead = tmphead; /* Store new index */
  325.  
  326. UCSR0B |= (1<<UDRIE0); /* Enable UDRE interrupt */
  327. }
  328.  
  329. void sendADCValue(void)
  330. {
  331. ADCSRA |= 1<<ADSC; // Start Conversion
  332. _delay_ms(1); // Wait for the interrupt to complete and then to check the if statement
  333. if (sendflag)
  334. {
  335. uint16_t adcData = ADCH;
  336. printf("ADC Value: %u\n", adcData);
  337. sendflag = 0;
  338. }
  339. }
  340. void readLightingSensor(void)
  341. {
  342. if (sendflag)
  343. {
  344. uint16_t adcData = ADCH;
  345. printf("%u\n", adcData);
  346. sendflag = 0;
  347. }
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement