//Tx code
char uart_dat = 83,uart_rct;
int i,j=3,k=0;
int _duty=175;
void main() {
TRISB = 0;
TRISA = 0xFF;
TRISD=1;
TRISC = 0x00; //port c as output
PORTA = 0x00;
PORTC = 0x00;
PORTB =0;
PWM1_Init(1000);
PWM1_start();
PWM1_Set_Duty(_duty);
PORTB = 1; //Run motor in clockwise
UART1_Init(9600);
Delay_ms(100);
for(i=2;i>=0;i--)
{
while(i>0) {
UART1_Write(uart_dat); // 83 = S for start
Delay_ms(2000);
i--;
}
delay_ms(1000);
}
delay_ms(50000);
TRISC =0xFF; // port c as input
UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
while (1) {
/*if (UART1_Data_Ready() == 1) { // if data is received
UART1_Read_Text(uart_rct, "$$", 25); // reads text until $$
}
}
} */
UART1_Init(9600);
Delay_ms(100);
if(UART1_Data_Ready() == 1)
{
uart_rct = UART1_Read();
if(uart_rct == 80)PORTB.F3=1;
else PORTB.F5=1;
uart_rct = '\0';
}
}
}
char uart_rd = 0;
int i,j,k=2;
void main() {
TRISB = 0x00;
TRISA = 0xFF;
TRISC = 0x80;
TRISD = 0x00;
PORTA = 0x00;
PORTC = 0x00;
PORTD = 0x00;
UART1_Init(9600);
Delay_ms(100);
do
{
if(UART1_Data_Ready() == 1) //checking for a start command
{
uart_rd = UART1_Read();
if(uart_rd == 83)PORTD.F0=1;
else PORTD.F0=0;
uart_rd = '\0';
}
}while(PORTD.F0==0); // start command obtained exit loop
for(i=5;i>=0;i--) //msb of counter
{
PORTB =i;
for(j=9;j>0;j--) //Lsb of counter
{
PORTD =j; //Lsb from port d
delay_ms(1000);
}
PORTD =0;
delay_ms(1000);
}
PWM1_Init(1000); // setup motor circuit
PWM1_start();
PWM1_Set_Duty(160);
do
{
if(PORTD.F0==0); // if counter completed proceed
{
do
{
if (PORTD.F0==0) // counter complete check just for convinience
{PORTB.F4 = 1; //Run motor in clockwise
delay_ms(1000);
PORTB.F4 = 0; // stop motor
TRISB.F6 = 1; // 6th pin as input for sensor
if(PORTB.F6==0) continue; // if no object goes to last of this brace
else if(PORTB.F6==1) // if object open gate
{
PORTB.F5 = 1; //Run motor in anti-clockwise
delay_ms(1000);
PORTB.F5 = 0; //stop motor
delay_ms(2000);
}
}
}while(PORTB.F6==1); // loops this until object is cleared , F6=0
PORTD.F6=1; // indication that everything clear
UART1_Init(9600); // send back pass command
delay_ms(100); // stabilise uart
while(k>0) { // k is initialized as 2 so send 2 P
UART1_Write(80); // send P
Delay_ms(2000);
k--;
}
}
} while(PORTD.F0==1);
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 char uart_rd = 0; unsigned int i = 0, j = 0, k = 2, pwmDuty = 160; void main() { INTCON = 0x00; ADCON0 = 0b11000001; ADCON1 = 0b11000110; T1CON = 0x00; T2CON = 0x07; SSPCON.F5 = 0; TRISE.F4 = 0; TRISA = 0xFF; TRISB = 0x00; TRISC = 0x80; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; UART1_Init(9600); Delay_ms(100); PWM1_Init(1000); //setup motor circuit PWM1_SetpwmDuty(pwmDuty); while(1) { while(1) { if(UART1_Data_Ready()) { uart_rd = UART1_Read(); if(uart_rd == 'S') { PORTD.F0 = 1; break; //start command obtained exit loop } else PORTD.F0 = 0; uart_rd = '\0'; } } PORTC = 0x02; //caution light starts for(i = 5; i >= 0; i--) { //msb of counter at PORTB PORTB = i; for(j = 9; j > 0; j--) { //Lsb of counter at PORTD PORTD = j; //Lsb from PORTD Delay_ms(1000); } PORTD = 0; Delay_ms(1000); } PORTB = 0x00; //set ports to low PORTD = 0x00; PWM1_start(); do { if(!PORTD) { //if counter completed proceed while(1) { if(!PORTD) { //counter complete check just for convinience PORTB = 0x10; //Run motor in clockwise Delay_ms(3000); PORTB = 0; //stop motor TRISB.F6 = 1; //6th pin as input for sensor if(PORTB.F6) { //if object open gate PORTB = 0x20; //Run motor in anti-clockwise Delay_ms(3000); PORTB = 0; //stop motor Delay_ms(3000); break; //break loop } } } PORTC = 0 ; //caution light stops PORTD = 0x20; //indication that everything clear while(k > 0) { //k is initialized as 2 so send 2 P UART1_Write('P'); //send P Delay_ms(2000); k--; } } }while(PORTD.F0); } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?