Smart ToyCar:
MikroC Code For Reciver://PIC16F877A
//12MHz
float reading=0 , act_reading=0, volt=0;
void Get_Volt(void)
{
adcon1=0b0000;
delay_ms(50);
reading=Adc_Read(0);
act_reading=(reading*5)/1024;
volt=act_reading*2.25; //volt_in
}
void Tone() //horn
{
Sound_Play(500, 10);
}
void Tone_back()
{
Sound_Play(2000, 300);
delay_ms(200);
}
void main()
{
TRISA = 0xFF;
porta = 0x00;
TRISC = 0x00; // PORT B as output port
portc = 0x00; // Set RB0 to high
TRISD = 0xDF;
portd = 0x00;
TRISB = 0x00;
portb = 0x00;
Sound_Init(&PORTC, 7); //sound Initialization
while(1)
{
if(portd.f4=1)
{
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=0;
PORTC.F5 = 0;
PORTC.F6 = 0;
//Charge
Get_Volt(void);
if(volt>9)
{
portd.f5=1;
}
else portd.f5=0;
}
else
{
portd.f5=0;
//headlight
if(portd.f6==1) portc.f4 = 1;
else portc.f4 = 0;
//forward
if(portd.f0==1 && portd.f2==0 && portd.f3==0)
{
PORTC.F0 = 1;
PORTC.F1 = 0;
PORTC.F2 = 1;
PORTC.F3 = 0;
PORTC.F5 = 0;
PORTC.F6 = 0;
portb.f0=0;
portb.f1=1;
}
//backward
if(portd.f1==1 && portd.f2==0 && portd.f3==0)
{
PORTC.F0 = 0;
PORTC.F1 = 1;
PORTC.F2 = 0;
PORTC.F3 = 1;
PORTC.F5 = 1;
PORTC.F6 = 1;
portb.f0=1;
portb.f1=0;
Tone_back();
}
//right
if(portd.f2==1)
{
PORTC.F0 = 0;
PORTC.F1 = 1;
PORTC.F2 = 1;
PORTC.F3 = 0;
PORTC.F5 = 0;
PORTC.F6 = 1;
portb.f0=1;
portb.f1=0;
Tone_back();
}
//left
if(portd.f3==1)
{
PORTC.F0 = 1;
PORTC.F1 = 0;
PORTC.F2 = 0;
PORTC.F3 = 1;
PORTC.F5 = 1;
PORTC.F6 = 0;
portb.f0=1;
portb.f1=0;
Tone_back();
}
//To Stop motor
if(portd.f0==0 && portd.f1==0 && portd.f2==0 && portd.f3==0)
{
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=0;
PORTC.F5 = 0;
PORTC.F6 = 0;
}
//horn
if(portd.f7==1)
Tone();
}
}
}
MikroC Code for Transmitter:
// PIC16F73 X-TAL= 8MHz
float reading=0 , act_reading=0;
float volt=0;
void Get_Volt(void);
void Tone() //volt
{
Sound_Play(3500, 20);
delay_ms(100);
}
void main()
{
TRISA = 1;
TRISB = 0;
TRISC = 0x00;
PORTB = 0;
UART1_Init(9600); // Initialize UART module at 9600bps
Delay_ms(100); // Wait for UART module to stabilize
Sound_Init(&PORTC, 0); //Sound initialization
while (1)
{ // Endless loop
UART1_Write(portb); //send data via UART
Delay_ms(500);
Get_Volt(void);
if(volt>=4.95)
{
portc.f1=1;
Tone();
}
else
portc.f1=0;
}
}
void Get_Volt(void)
{
adcon1=0b000;
delay_ms(50);
reading=Adc_Read(0);
act_reading=(reading*20.11)/1024;
volt=act_reading;
}