Multisim and Ultiboard

cancel
Showing results for 
Search instead for 
Did you mean: 

Combine bluetooth with pic 16f877a

Hi guys, i'm working on my project with pic16f877a. I intend create a traffic light system by using bluetooth to control. It has 2 way to work, first way is automatic traffic light, when i crack the switch, it will turn off then change to control by hand. I don't know how to combine the bluetooth code when it changed to control by hand. Please help, here is my code, thank you everyone.

 

#include <16f877a.h>
#fuses nowdt,hs,put,nolvp,noprotect
#use delay (clock=20000000)
#use fast_io(D)
#use RS232 (BAUD=9600,BITS=8,PARITY=N,XMIT=Pin_C6,RCV=Pin_C7)
#define SH_CP pin_c0 
#define DS pin_c1 
#define ST_CP pin_c2
#define ground pin_c3
#define D1 pin_b0 
#define X1 pin_b1 
#define V1 pin_b2
#define D2 pin_b3 
#define X2 pin_b4 
#define V2 pin_b5 


unsigned char ma[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90} ;
int Y1,Y2,Y3,Y4;
char sym;

void HC595out (int DATA) 
{ int window=0x80;
int y;
for (y=0;y<8;y++)
{ if ((DATA & window)==0)
output_low(DS);
else
output_high(DS);
output_low(SH_CP);
output_high(SH_CP);
window=window>>1; 
}
}

void xuatled()
{
HC595out(Y1);
HC595out(Y2);
HC595out(Y3);
HC595out(Y4);
output_low(ST_CP);
output_high(ST_CP); 
output_low(ground);
}

void GL1 (int T1)
{
Y1=(ma[T1%10]);
Y2=(ma[T1/10]);
}

void GL2 (int T2)
{
Y3=(ma[T2%10]);
Y4=(ma[T2/10]);
}

void sangngatu()
{
set_tris_b(0xff);
set_tris_c(0x00);
set_tris_d(0x00);
output_b(0x00);

int i,j,k,m;
//xanh 1 do 2
if(input(pin_C5))
{i=0;
if (i<31)
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_low(D2);
output_low(X1);
while (i<31)
{
GL1(30-i);
GL2(36-i);
xuatled();
i++;
delay_ms(200);
if (!input(pin_C5))
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
i=31;j=31;k=31;m=31;}
if (i>30)
{i=31;
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
j=0;
}
}}

//vang 1 do 2
if (j<6)
{
output_high(X1);
output_high(X2);
output_high(V2);
output_high(D1);
output_low(V1);
output_low(D2);
while (j<6)
{
GL1(5-j);
GL2(5-j);
xuatled();
j++;
delay_ms(200);
if (!input(pin_C5))
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
i=6;j=6;k=6;m=6;}
if (j>6)
{j=6;
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
k=0;
}
}}

//do 1 xanh 2
if (k<31)
{
output_high(V1);
output_high(D2);
output_high(X1);
output_high(V2);
output_low(X2);
output_low(D1);
while (k<31)
{
GL1(36-k);
GL2(30-k);
xuatled();
k++;
delay_ms(200);
if (!input(pin_C5))
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
i=31;j=31;k=31;m=31;}
if (k>30)
{k=31;
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
m=0;
}
}}

//do 1 vang 2
if (m<6)
{
output_high(X1);
output_high(X2);
output_high(V1);
output_high(D2);
output_low(V2);
output_low(D1);
while (m<6)
{
GL1(5-m);
GL2(5-m);
xuatled();
m++;
delay_ms(200);
if (!input(pin_C5))
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
i=6;j=6;k=6;m=6;}
if (m>6)
{m=6;
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
}
}}
}
}

void demthucong()
{
output_high(X2);
output_high(V1);
output_high(V2);
output_high(D1);
output_high(D2);
output_high(X1);
GL1(0);
GL2(0);
xuatled();
}

void sangX1D2()
{
output_low(X1);
output_low(D2);
}

void sangX2D1()
{
output_low(X2);
output_low(D1);
}

void tat()
{
output_b(0xff);
output_C(0xff);
output_D(0xff);
}

void main()
{
while(TRUE)
{

if (input(pin_C5))
sangngatu();
if (!input(pin_C5))
demthucong();
delay_ms(500);
tat();
}
}

 

0 Kudos
Message 1 of 1
(73 Views)