rendered paste body#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h>
//variables
#define portio PIND
#define portiow PORTD
#define rxled PD7
#define rele PD6
#define btnsw PD5
#define triac PD4
#define MAX_BUFFSIZE 3
#define TRUE 1
#define FALSE 0
unsigned char receiveChar(void )
{
unsigned char ch = UDR;
return ch;
}
void sendChar(char data)
{
while( !(UCSRA&(1<<UDRE)) )
;
// put the data in the USART data register
UDR = data;
UCSRA=UCSRA|0x40;
}
void USART_Flush( void )
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) )
dummy = UDR;
}
void usartInit(unsigned int ubrr)
{
cli(); // disable interrupts while adjusting baudrate
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<<TXCIE)|(0<<UDRIE)|(0<<UCSZ2);
UCSRC = (1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
UBRRH = (unsigned char) (ubrr>>8);
UBRRL = (unsigned char) ubrr;
sei(); // Enable interrupts
}
volatile unsigned char usart[MAX_BUFFSIZE];
volatile unsigned char usart1[MAX_BUFFSIZE];
//volatile unsigned char usart2[MAX_BUFFSIZE];
//volatile unsigned char usart3[MAX_BUFFSIZE];
volatile unsigned char rx_count = 0;
volatile unsigned char GotMessage = FALSE;
short int ovf0cnt = 0; //value for timer0 cycle
short int int0cnt = 0;
volatile unsigned int timer = 0;
volatile unsigned char timer_flag = FALSE;
//short int i = 0; //value for pwm storing
volatile short int btnsw_right = 0;
//int count = 0;
int main (void)
{
TIMSK |= (1<<TOIE1 | 1 << TOIE0); //enable interrupt from Timer0
MCUCR |= (1<<ISC00 | 1<<ISC01);
TCCR0 = (1<<CS02 | 0<<CS01 | 0<<CS00);
TCCR2 = 0x61; //set TCCR2 to the above values (0x01100001b)
OCR2 = 0x00; //set OCR2 to overflow at XX
DDRB = 0x08; //as of right now, only enable the PWM pin as output
DDRD = (1 << PD7 | 1 << PD6 | 0<<PD5 | 1<<PD4 | 0<<PD2);
PORTD |= (1 << PD5 | 1<<PD4);
usartInit(51);
//PORTD = 1<<PD6;
while(1)
{
if((btnsw_right == 0) && ((portio & (1<<btnsw))))//0x20)))
btnsw_right = 1;
if(GotMessage){
// switch(usart[0])
// {
// case '1': { usart1 = usart[1]; timer = usart[2]*100;}// break;
// }
switch(usart1[1])
{
case '1': portiow |= (1 << rele); break;
case '2': portiow &= ~(1 << rele); break;
case '3': timer = 450; break;
case '4': timer = 450; break;
}
GotMessage = rx_count = FALSE;
// }
}
/* if(GotMessage){
switch(usart[0])
{
case '1': { usart1[0] = usart[0]; usart1[1] = usart[1]; usart1[2] = usart[2];}// break;
// }
switch(usart1[2])
{
case '1': portiow |= (1 << rele); break;
case '2': portiow &= ~(1 << rele); break;
case '3':
case '4': timer = 450; break;
} break;
GotMessage = rx_count = FALSE;
}
}*/
}
}
ISR(TIMER0_OVF_vect)
{
if (++ovf0cnt == 4)
{
if((btnsw_right == 1) && (!(portio & (1<<btnsw))))
{
portiow ^= (1 << rele);
btnsw_right = 0;
}
ovf0cnt = 0;
}
if(timer)
// if(usart1 == '3' || usart1 == '4')
{
OCR2=0xff;
if(--timer == 0)
{
portiow ^= (1 << rxled);
//sendChar(usart1[1]);
if(usart1[1] == '3'){
portiow |= (1 << rele);
// sendChar('S');
// sendChar('3');
}else if(usart1[1] == '4') portiow &= ~(1 << rele);
}
// portiow ^= (1 << rxled);
}
}
ISR(USART_RXC_vect)
{
if((rx_count < MAX_BUFFSIZE) && RXC && !GotMessage)
{
usart[rx_count++] = receiveChar();
if(rx_count == MAX_BUFFSIZE)
{
sendChar(usart[1]);
//portiow ^= (1 << rxled);
GotMessage = TRUE;
// for(int i = 0; i < rx_count; i++)
// usart1[i] = usart[i];
/* usart1[0] = usart[0];
usart1[1] = usart[1];
usart1[2] = usart[2];
*/
switch(usart[0])
{
case '1': { usart1[1] = usart[1]; usart1[2] = usart[2];} break; //timer = usart[2]*100;}// break;
}
/*
switch(usart1[0])
{
case '1': portiow |= (1 << rele); break;
case '2': portiow &= ~(1 << rele); break;
case '3': timer = 450; break;
case '4': timer = 450; break;
} break;
GotMessage = rx_count = FALSE;
}*/
}
}
}