C 20
Homework-5-avr By Hightechrobo on 28th August 2025 10:26:37 AM
  1. #include <mega32.h>
  2. #include <delay.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <bool.h>
  7. unsigned char rx_data[20];
  8. unsigned char str[20];
  9. unsigned char rx_buffer;
  10. unsigned char pwm ;
  11. float  voltage;
  12. bool enter = 0 ;
  13. void clear_str()
  14. {
  15.     char i ;
  16.     rx_buffer = 0;
  17.     enter = 0;
  18.     for(i=0 ; i < 20 ; i++)
  19.         rx_data[i] = 0;
  20. }
  21.  
  22.  
  23. unsigned char convert(unsigned char *myData) {
  24.     int result = 0;
  25.     int i = 0;
  26.  
  27.     while (myData[i-1]  != '='   && myData[i] <= '9'  && myData[i] >= '0')
  28.     i++;
  29.     while (myData[i]    != '%'   && myData[i] <= '9'  && myData[i] >= '0'){
  30.             result = result * 10 + (myData[i] - '0');
  31.     i++;}
  32.        
  33.    
  34.  
  35.     return result;
  36. }
  37.  
  38. // USART Receiver interrupt service routine
  39. interrupt [USART_RXC] void usart_rx_isr(void)
  40. {
  41.    if(UDR == '\r')
  42.    enter = 1 ;
  43.    else{
  44.    rx_data[rx_buffer] = UDR;  
  45.    putchar(rx_data[rx_buffer]);
  46.    rx_buffer ++ ;
  47.    }
  48.    if(rx_buffer > 20)
  49.     clear_str();
  50. }
  51.  
  52.  
  53. unsigned int adc_data;
  54. // Voltage Reference: AREF pin
  55. #define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR))
  56.  
  57. // ADC interrupt service routine
  58. interrupt [ADC_INT] void adc_isr(void)
  59. {
  60. // Read the AD conversion result
  61. adc_data=ADCW;
  62. }
  63.  
  64. // Read the AD conversion result
  65. // with noise canceling
  66. unsigned int read_adc(unsigned char adc_input)
  67. {
  68. ADMUX=adc_input | ADC_VREF_TYPE;
  69. // Delay needed for the stabilization of the ADC input voltage
  70. delay_us(10);
  71. #asm
  72.     in   r30,mcucr
  73.     cbr  r30,__sm_mask
  74.     sbr  r30,__se_bit | __sm_adc_noise_red
  75.     out  mcucr,r30
  76.     sleep
  77.     cbr  r30,__se_bit
  78.     out  mcucr,r30
  79. #endasm
  80. return adc_data;
  81. }
  82.  
  83. void init();
  84.  
  85. void main(void)
  86. {
  87.  
  88. init();
  89. puts("UART Console");
  90. putchar('\r');
  91. puts("Command:");
  92. while (1)
  93.       {
  94.       // Place your code here  
  95.        
  96.       if (enter) {
  97.      
  98.      
  99.         ///////////////////////////////////////////relay
  100.         if (strstr(rx_data, "relay ON")) {
  101.         PORTD.7=1;
  102.         puts("\rrelay is on");
  103.         }
  104.         if (strstr(rx_data, "relay OFF")){
  105.         PORTD.7=0;    
  106.         puts("\rrelay is off");
  107.         }
  108.         ///////////////////////////////////////////PWM
  109.         if (strstr(rx_data, "pwm=")){
  110.             pwm = convert(rx_data);
  111.             OCR0 = (pwm * 255) / 100;  
  112.             sprintf(str,"pwm is %d %%",pwm);
  113.         putchar('\r');
  114.         puts(str);
  115.         }
  116.         ///////////////////////////////////////////voltage
  117.         if (strstr(rx_data, "voltage")){  
  118.         voltage = ( read_adc(0) * 5.0 ) /1023.0 ;
  119.         sprintf(str,"voltage=%0.2f",voltage);
  120.         putchar('\r');
  121.         puts(str);      
  122.         }
  123.                
  124.        
  125.        
  126.        
  127.        
  128.         clear_str();
  129.         puts("\rCommand:");
  130.         }
  131.  
  132.       }
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139. void init()
  140. {
  141.  
  142. // Input/Output Ports initialization
  143. // Port A initialization
  144. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  145. DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
  146. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  147. PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
  148.  
  149. // Port B initialization
  150. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=Out Bit2=In Bit1=In Bit0=In
  151. DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (1<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
  152. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=0 Bit2=T Bit1=T Bit0=T
  153. PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
  154.  
  155. // Port C initialization
  156. // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  157. DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
  158. // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  159. PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
  160.  
  161. // Port D initialization
  162. // Function: Bit7=Out Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
  163. DDRD=(1<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
  164. // State: Bit7=0 Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
  165. PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
  166.  
  167. // Timer/Counter 0 initialization
  168. // Clock source: System Clock
  169. // Clock value: 125.000 kHz
  170. // Mode: Fast PWM top=0xFF
  171. // OC0 output: Non-Inverted PWM
  172. // Timer Period: 2.048 ms
  173. // Output Pulse(s):
  174. // OC0 Period: 2.048 ms Width: 0 us
  175. TCCR0=(1<<WGM00) | (1<<COM01) | (0<<COM00) | (1<<WGM01) | (0<<CS02) | (1<<CS01) | (1<<CS00);
  176. TCNT0=0x00;
  177. OCR0=0x00;
  178.  
  179. // Timer/Counter 1 initialization
  180. // Clock source: System Clock
  181. // Clock value: Timer1 Stopped
  182. // Mode: Normal top=0xFFFF
  183. // OC1A output: Disconnected
  184. // OC1B output: Disconnected
  185. // Noise Canceler: Off
  186. // Input Capture on Falling Edge
  187. // Timer1 Overflow Interrupt: Off
  188. // Input Capture Interrupt: Off
  189. // Compare A Match Interrupt: Off
  190. // Compare B Match Interrupt: Off
  191. TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
  192. TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10);
  193. TCNT1H=0x00;
  194. TCNT1L=0x00;
  195. ICR1H=0x00;
  196. ICR1L=0x00;
  197. OCR1AH=0x00;
  198. OCR1AL=0x00;
  199. OCR1BH=0x00;
  200. OCR1BL=0x00;
  201.  
  202. // Timer/Counter 2 initialization
  203. // Clock source: System Clock
  204. // Clock value: Timer2 Stopped
  205. // Mode: Normal top=0xFF
  206. // OC2 output: Disconnected
  207. ASSR=0<<AS2;
  208. TCCR2=(0<<PWM2) | (0<<COM21) | (0<<COM20) | (0<<CTC2) | (0<<CS22) | (0<<CS21) | (0<<CS20);
  209. TCNT2=0x00;
  210. OCR2=0x00;
  211.  
  212. // Timer(s)/Counter(s) Interrupt(s) initialization
  213. TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
  214.  
  215. // External Interrupt(s) initialization
  216. // INT0: Off
  217. // INT1: Off
  218. // INT2: Off
  219. MCUCR=(0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
  220. MCUCSR=(0<<ISC2);
  221.  
  222. // USART initialization
  223. // Communication Parameters: 8 Data, 1 Stop, No Parity
  224. // USART Receiver: On
  225. // USART Transmitter: On
  226. // USART Mode: Asynchronous
  227. // USART Baud Rate: 9600
  228. UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
  229. UCSRB=(1<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
  230. UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
  231. UBRRH=0x00;
  232. UBRRL=0x33;
  233.  
  234. // Analog Comparator initialization
  235. // Analog Comparator: Off
  236. // The Analog Comparator's positive input is
  237. // connected to the AIN0 pin
  238. // The Analog Comparator's negative input is
  239. // connected to the AIN1 pin
  240. ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
  241.  
  242. // ADC initialization
  243. // ADC Clock frequency: 62.500 kHz
  244. // ADC Voltage Reference: AREF pin
  245. // ADC Auto Trigger Source: ADC Stopped
  246. ADMUX=ADC_VREF_TYPE;
  247. ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
  248. SFIOR=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);
  249.  
  250. // SPI initialization
  251. // SPI disabled
  252. SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
  253.  
  254. // TWI initialization
  255. // TWI disabled
  256. TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE);
  257.  
  258. // Global enable interrupts
  259. #asm("sei")
  260. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.