Objective:
- To design a program proportional with the flow chart of the project
- To master C compiler software
- To master C language
Content:
PCWH C-Compiler Software
PCWH powerful C-Compiler that
developed by CCS Co. USA. The software is designed mainly for 8 bit chip
microcontroller embedded system. It supports a wide range of PIC
micro-controllers, including PIC10, PIC12, PIC14, PIC16, and PIC18. The
software also has broad library functions at which programmer can access.
In general, the PCWH C-Compiler allows
user:
- Write the C code using advanced code editor.
- By using "include" function, accessing or processing of code become even faster.
- Control the execution of the program at which the "error" will be reported when bugs are detected.
- Samples of program also provided in the helps file.
To used the software creating a project,
make sure a copy of PCWH available in the computer. User can download a 30 days
trial copy from http://ccsinfo.com/ccsfreedemo.php.
Follow steps by steps on the instruction during installation process and
finally user will see an icon of PCWH appears on the desktop of the computer.
Steps to use PCWH C-Compiler
1.
Open
PCW C-Compiler Start->All
Programs->PIC -C->PIC C Compiler.
2.
‘File’
-> ‘New’ to start a new file.
3.
Save
the file as .c file, it is advisable to put the main file name within 8 chars
length, extended file name must be .c, e.g. myprog1.c.
4.
Type
/ Edit your program.
5.
Save
program before compile.
6.
Compile
your program using F9.
7.
If
any error occurs, please check your program and compile again. Otherwise you
won’t get your .hex file.
For the project, by using this software, there are two program need to be design which is for the transmitter and the receiver. These two programs are necessary to make sure the project function smoothly. After countless trial and compiling, finally the programs works and ready to use.
Program for transmitter (PIC 16F876A)
#include <16f876A.h> //use pic16f876a library
#use delay(clock=20000000) //set pic clock speed as 20mhz
#use rs232(baud=4800,xmit=PIN_C6,rcv=PIN_C7,PARITY=N) //set serial interfacing
#fuses hs,protect,nowdt,nolvp //default setting
#byte PORTA=5 //define PORT A address
#byte PORTB=6 //define PORT B address
#byte PORTC=7 //define PORT C address
void main()
{
int i=0;
//set i/o for each pin
set_tris_a(0b00000000);
set_tris_b(0b00000000);
set_tris_c(0b10000000);
output_high(pin_c2); //on led
delay_ms(1000);
output_low(pin_c2); //off led
delay_ms(1000);
do
{
output_high(pin_c2); //on led
//send data out
for(i=0;i<10;i++)
{
putc('(');
putc('1');
putc(')');
delay_ms(10);
}
output_low(pin_c2); //off led
delay_ms(3000);
}while(1);
}
Program for receiver (PIC 16F877A)
#include <16f877A.h> //use pic16f877a
#use delay(clock=20000000) //set pic speed 20mhz
#fuses hs,noprotect,nowdt,nolvp //default setting
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, parity=N) //set serial interfacing
#byte PORTA=5
#byte PORTB=6
#byte PORTC=7
#byte PORTD=8
#byte PORTE=9
int rx_temp1=0;
int rx_temp3=0;
int rcvdata;
int mystat=0;
//receive data from transmitter
#int_rda
void serial_isr()
{
rx_temp1=getch();
if(rx_temp1=='(')
{
rcvdata=getc();
rx_temp3=getch();
if(rx_temp3==')')
{
mystat=1;
}
}
rx_temp1=0;
rx_temp3=0;
}
void main()
{
int id1sta=0;
int id1count=0;
set_tris_a(0b00000000); //set i/o pin for port a
set_tris_b(0b00000000); //set i/o pin for port b
set_tris_c(0b10011000); //set i/o pin for port c
set_tris_d(0b00000110); //set i/o pin for port d
set_tris_e(0b00000000); //set i/o pin for port e
//initialize interrupt
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
output_low(pin_d5); //stop motor1
output_low(pin_d4); //stop motor2
output_low(pin_c5); //lock door
output_high(pin_c0); //on blue led
output_high(pin_c1); //on white led1
output_high(pin_c2); //on white led2
delay_ms(1000);
output_low(pin_c0); //off blue led
output_low(pin_c1); //off white led1
output_low(pin_c2); //off white led2
delay_ms(1000);
do
{
//if RF receiver received valid data
if(mystat==1)
{
mystat=0;
if(rcvdata=='1') //if '1' received
{
id1sta=1;
id1count=0;
}
}
else
{
if(id1count<6) //if transmitter lost less than 3sec
{
id1count=id1count+1; //increase timer
}
else //if 3sec reached
{
id1sta=0;
}
}
if(id1sta==1) //if transmitter in range
{
output_high(pin_c5); //unlock door
output_high(pin_c0); //on blue led
delay_ms(250);
output_low(pin_c0); //off blue led
delay_ms(250);
if(input(pin_d1)==0) //if on white led button pressed
{
output_high(pin_c1); //on white led1
output_high(pin_c2); //on white led2
}
if(input(pin_d3)==0) //if off white led button pressed
{
output_low(pin_c1); //off white led1
output_low(pin_c2); //off white led2
}
if(input(pin_d2)==0) //if on motor button pressed
{
output_high(pin_d5); //run motor1
output_high(pin_d4); //run motor2
}
if(input(pin_c4)==0) //if off motor button pressed
{
output_low(pin_d5); //stop motor1
output_low(pin_d4); //stop motor2
}
}
else //if transmitter out of range
{
output_low(pin_c1); //off white led1
output_low(pin_c2); //off white led2
output_low(pin_c5); //lock door
output_low(pin_d5); //stop motor1
output_low(pin_d4); //stop motor2
}
delay_ms(500);
}while(1);
}
Analysis:
The program on receiver circuit are longer than the program on the transmitter circuit because the transmitter job is only send a signal to the receiver when the transmitter is on the range, while the receiver circuit do all the works such as running the motor, switch, led's.
Conclusion:
There are numbers of the software that can be use to design a program. C compiler has been use is because this software is quite simple and easy to use. And with program and circuit is already design, purchasing the component will be the next step of the project
No comments:
Post a Comment