#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))
volatile uint8_t my_flag; //flag created to draw diverter
volatile uint8_t my_flagSC; //flag created to stop conveyor
void usart_init(void);
void usart_init(void)
{
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE) ; //ENABLE RECEIVER,TRANSMITTER AND RECEIVE INTERRUPT
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL); //USE 8 BIT DATA TRANSFER AND ONE START ONE STOP BITS WITH 9600 BAUDRATE
UBRRL = 0x33;
}
int main(void)
{
DDRA = 0xFF; //PORT A IS OUTPUT
usart_init();
GICR = (1<<INT0)|(1 << INT2); //ENABLE EXTERNAL INTERRUPT 0&2 ON PORT D^2 & B^2
MCUCSR = (0 << ISC2); //whenever the interrupt 2 is high to low(negative edge triggered)
MCUCR=0x02;//MAKE INT0 FALLING EDGE TRIGGERED
sei();
while(1) //continue the following process
{
if(my_flag == 1) // check if flag is 1 and execute the following code for running diverter motor after rs232 interrupt
{
my_flag = 0; // reset the flag
SETBIT(PORTA, 0); // set bit0 of PORTA to 1
_delay_ms(5000); // delay 1000ms or as much as you want
CLEARBIT(PORTA, 0); // set bit0 of PORTA to 0
}
}
}
ISR(INT2_vect)
{
my_flagSC=1;
usart_send('C'); //JUMP TO VOID USART_SEND TO SEND THE ASCII CHRACTER C to matlab from 15th pin of UC FOR CAPTURING FRAME FROM CAMERA
}
void usart_send(unsigned char g)
{
UDR = g; //PUT CHRACTER B IN USART DATA REGISTER TO TRANSMITT
if(my_flagSC==1)
{
my_flagSC=0;
SETBIT(PORTA,4);// set bit4 of PORTA to 1
_delay_ms(5000); // delay 10000ms or as much as you want to stop conveyor
CLEARBIT(PORTA, 4); // set bit0 of PORTA to 0
}
}
ISR(USART_RXC_vect) //RECEIVE INTERRUPT
{
unsigned int d;
d = UDR;
my_flag=1; // set the flag when the interrupt occurs
}
ISR(INT0_vect)
{
usart_send('D'); //JUMP TO VOID USART_SEND TO SEND THE ASCII CHRACTER B to matlab from 15th pin of UC
}