Hello all,
I am currently working on a project to establish to check the frequency of two input signals and am finding it hard to get the code working in multisim. The initial code is just to have the two timers set up as counters and a delay of 100microsecs before(hopefully) an interrupt occurs and checks the difference of the frequencies.I would like to incorporate a buzzer and an lcd but would like to walk befor i can run-I personally find the programming difficult to get my head round.Any help on this matter would be greatly appreciated and I will put up my c files for the timer and lcd(Cant say i trust either).
Thanks for any input at all.
#include<htc.h>
#define TRUE 1
#define FALSE 0
#define LED P12;
void delay(void);//50us delay;
static void delay1(void){//set up for 1ms delay
char c;
for(c=0;c<20;c++){
delay();
}
}
#define On_Off= P10;//On-Off switch on P1^0(Starts device)
//function to see if switched is pressed
bit SwitchOn_Off(void);
//50ms debounce function
void debounce (void);
//function to count the pulses received on the timer input pins(T0 and T1)
void Pulsecount(void);
void delay(void);//50us delay;
void main (void){
while (TRUE){
if (SwitchOn_Off()==TRUE){
LED=0;
Pulsecount();//initiate count
}
else{}//do nothing
}
}
bit SwitchOn_Off(void){
if(On_Off==FALSE){//If switched is closed(0)
debounce();//carry out switch debounce(delay Procedure)
return TRUE;//Returns TRUE to allow operation to carry on
}
return FALSE;//function returns a FALSE
}
void debounce (void){
TL0=0xb0;
TH0=0x3c;
TMOD=0x01;
TR0=1;
TF0=0;
/*sets timer0 as interval timer and loads the decimal value
15536 in to it;;leading to it being incremeneted 50000 times
before it overflows;;ie TF=1(50ms)*/
while (TF0==FALSE){}
TR0=FALSE;
TF0=FALSE;
}
void Pulsecount(void){
while(TRUE)
T0=1;//set T0 as input pin(therefore increments Timer0 as pulse goes from low to high
T1=1;//set T1 as input pin(therefore increments Timer1 as pulse goes from low to high
TMOD=0x55;//sets Timers in 16bit interval timer mode
//clear any values in timers
TH1=0;
TL1=0;
TH0=0;
TL0=0;
TR1=TRUE;//start timer1
TR0=TRUE;//start timer0
delay1();
TR1=FALSE;
TR0=FALSE;
}
void delay (void){
char c;
for(c=0;c<16;c++);//50us delay
}
#include <reg51.h>
#define LCD_PORT P0 //LCD_PORT = total pins on port o
sbit Busy = LCD_PORT^7; //Busy flag
sbit E = P2^2; //enable pin
sbit RS = P2^0; //register select pin
sbit RW = P2^1; //read/write pin
/*This function negates the need for a delay loop as the use of the busy flag
gives a delay for almost the exact time necessart to process the commands */
void LCDbusy(){
Busy=1; //make input (Busy Flag)
E=1;
RS=0; //command register selected
RW=1; //read
while(Busy){ //keep reading busy flag until changes to zero
E=1;
E=0; //until busy flag changes introduce high to low enable
}
}
/*This funtion retrieves the value on singular port pins*/
static bit getBit(char c, char bitNumber) {
return (c >> bitNumber) & 1;
}
//-------------------------------
void Home(void) {
//returns both the display and cursor to original position
RS = 0;
RW=0;
LCD_PORT=0x02; //00000010
E = 1;
E = 0;
LCDbusy();
}
void ClearDisplay(void) {
//clears entire display and sets ddram to 0
RS = 0;
RW=0;
LCD_PORT=0x01;//00000001
E = 1;
E = 0;
LCDbusy();
// total of 1.65 ms - data sheet states clearing the display takes 1.52 ms
}
void EntryModeSet(void) {
//Sets cursor move direction and specifies shift in
//display
LCD_PORT=0x06;//000001110
//"I/D"set to increment (cursor moves to the right)(DB1)
//"S"=0 so tyhe display is not shifted at this point(DB0)
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void displayOnOffControl(void) {
//Turns on both display and cursor
LCD_PORT=0x0F;//00001111
//"D"=1 turning display on(DB3)
//"C"=1 turning cursor on(DB2)
//"B"=1 blinking turned on (DB1)
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void cursorOrDisplayShift(void) {
//moves cursor and shifts the display to the left
LCD_PORT =0x18;//00001000
//DB3=1; //SC=1 all display moves
//DB2=0;//"R/L" =1 shift to right,,0 to left
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void functionSet(void) {
//Sets to 8-bit operation/1 display lines/and 5*8
// characters
RS=0;
RW=0;
LCD_PORT=0x30;//00110000
//"DL"=1,,Data length = 8bits(DB5)
//"N"=0,1 display lines(DB4)
//"F"=0,,5*8dots(DB3)
E = 1;
E = 0;
LCDbusy();
}
void setDdRamAddress(char address) {
RS = 0;
LCD_PORT=address;
E = 1;
E = 0;
LCDbusy();
}
void sendChar(char c) {
LCD_PORT=c;
RS = 1;
E = 1;
E = 0;
LCDbusy();
}
// -- End of LCD Module instructions
// --------------------------------------------------------------------
void sendString(char* str) {
int index = 0;
while (str[index] != 0) {
sendChar(str[index]);
index++;
}
}
///This function initializes the display
void initialize(void){
functionSet();
displayOnOffControl();
ClearDisplay();
EntryModeSet();
}
I am currently working on a project to establish to check the frequency of two input signals and am finding it hard to get the code working in multisim. The initial code is just to have the two timers set up as counters and a delay of 100microsecs before(hopefully) an interrupt occurs and checks the difference of the frequencies.I would like to incorporate a buzzer and an lcd but would like to walk befor i can run-I personally find the programming difficult to get my head round.Any help on this matter would be greatly appreciated and I will put up my c files for the timer and lcd(Cant say i trust either).
Thanks for any input at all.
#include<htc.h>
#define TRUE 1
#define FALSE 0
#define LED P12;
void delay(void);//50us delay;
static void delay1(void){//set up for 1ms delay
char c;
for(c=0;c<20;c++){
delay();
}
}
#define On_Off= P10;//On-Off switch on P1^0(Starts device)
//function to see if switched is pressed
bit SwitchOn_Off(void);
//50ms debounce function
void debounce (void);
//function to count the pulses received on the timer input pins(T0 and T1)
void Pulsecount(void);
void delay(void);//50us delay;
void main (void){
while (TRUE){
if (SwitchOn_Off()==TRUE){
LED=0;
Pulsecount();//initiate count
}
else{}//do nothing
}
}
bit SwitchOn_Off(void){
if(On_Off==FALSE){//If switched is closed(0)
debounce();//carry out switch debounce(delay Procedure)
return TRUE;//Returns TRUE to allow operation to carry on
}
return FALSE;//function returns a FALSE
}
void debounce (void){
TL0=0xb0;
TH0=0x3c;
TMOD=0x01;
TR0=1;
TF0=0;
/*sets timer0 as interval timer and loads the decimal value
15536 in to it;;leading to it being incremeneted 50000 times
before it overflows;;ie TF=1(50ms)*/
while (TF0==FALSE){}
TR0=FALSE;
TF0=FALSE;
}
void Pulsecount(void){
while(TRUE)
T0=1;//set T0 as input pin(therefore increments Timer0 as pulse goes from low to high
T1=1;//set T1 as input pin(therefore increments Timer1 as pulse goes from low to high
TMOD=0x55;//sets Timers in 16bit interval timer mode
//clear any values in timers
TH1=0;
TL1=0;
TH0=0;
TL0=0;
TR1=TRUE;//start timer1
TR0=TRUE;//start timer0
delay1();
TR1=FALSE;
TR0=FALSE;
}
void delay (void){
char c;
for(c=0;c<16;c++);//50us delay
}
#include <reg51.h>
#define LCD_PORT P0 //LCD_PORT = total pins on port o
sbit Busy = LCD_PORT^7; //Busy flag
sbit E = P2^2; //enable pin
sbit RS = P2^0; //register select pin
sbit RW = P2^1; //read/write pin
/*This function negates the need for a delay loop as the use of the busy flag
gives a delay for almost the exact time necessart to process the commands */
void LCDbusy(){
Busy=1; //make input (Busy Flag)
E=1;
RS=0; //command register selected
RW=1; //read
while(Busy){ //keep reading busy flag until changes to zero
E=1;
E=0; //until busy flag changes introduce high to low enable
}
}
/*This funtion retrieves the value on singular port pins*/
static bit getBit(char c, char bitNumber) {
return (c >> bitNumber) & 1;
}
//-------------------------------
void Home(void) {
//returns both the display and cursor to original position
RS = 0;
RW=0;
LCD_PORT=0x02; //00000010
E = 1;
E = 0;
LCDbusy();
}
void ClearDisplay(void) {
//clears entire display and sets ddram to 0
RS = 0;
RW=0;
LCD_PORT=0x01;//00000001
E = 1;
E = 0;
LCDbusy();
// total of 1.65 ms - data sheet states clearing the display takes 1.52 ms
}
void EntryModeSet(void) {
//Sets cursor move direction and specifies shift in
//display
LCD_PORT=0x06;//000001110
//"I/D"set to increment (cursor moves to the right)(DB1)
//"S"=0 so tyhe display is not shifted at this point(DB0)
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void displayOnOffControl(void) {
//Turns on both display and cursor
LCD_PORT=0x0F;//00001111
//"D"=1 turning display on(DB3)
//"C"=1 turning cursor on(DB2)
//"B"=1 blinking turned on (DB1)
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void cursorOrDisplayShift(void) {
//moves cursor and shifts the display to the left
LCD_PORT =0x18;//00001000
//DB3=1; //SC=1 all display moves
//DB2=0;//"R/L" =1 shift to right,,0 to left
RS = 0;
RW=0;
E = 1;
E = 0;
LCDbusy();
}
void functionSet(void) {
//Sets to 8-bit operation/1 display lines/and 5*8
// characters
RS=0;
RW=0;
LCD_PORT=0x30;//00110000
//"DL"=1,,Data length = 8bits(DB5)
//"N"=0,1 display lines(DB4)
//"F"=0,,5*8dots(DB3)
E = 1;
E = 0;
LCDbusy();
}
void setDdRamAddress(char address) {
RS = 0;
LCD_PORT=address;
E = 1;
E = 0;
LCDbusy();
}
void sendChar(char c) {
LCD_PORT=c;
RS = 1;
E = 1;
E = 0;
LCDbusy();
}
// -- End of LCD Module instructions
// --------------------------------------------------------------------
void sendString(char* str) {
int index = 0;
while (str[index] != 0) {
sendChar(str[index]);
index++;
}
}
///This function initializes the display
void initialize(void){
functionSet();
displayOnOffControl();
ClearDisplay();
EntryModeSet();
}