wang tianwei
Newbie level 1

off_time_counter
hi, i am using pic 16f877. and below is a program. Can anyone tell me how should i know the reference voltage as they are all hex numbers. thanks a lot
//Sample program for AdvFamLab
//Include required header files here
#include<pic1687x.h>
#include<pic.h>
//Declare global variables here
volatile int on_time_counter, off_time_counter;
volatile unsigned char light=0;
int timer_value=0xFE;
//Declare functions to be used in the program, i.e.
//void initialize_IO_ports(void)
//void initialize_ADC(void)
//int read_ADC_channel(unsigned int)
//void initialize_timer1(int)
//Put the body of all the functions
void initialize_IO_ports(void)
{
//set the digital IO ports as per requirement
TRISA = 0xFF ; //portA as input
TRISB = 0xFF ; //portB as input
TRISC = 0x00 ; //portC as output
TRISD = 0x00 ; //portD as output
ADCON1= 0x82 ; //set PortE as digital io and portA as analog, and VDD and VSS; ADC output is right-justified.
//clear the output ports at the beginning
PORTD = 0x00 ; //clear portD
PORTC = 0x00 ; //clear portC
}
void initialize_ADC(void)
{
//enable the ADC
ADON=1;
//set clock source for the ADC
ADCS1=0;
ADCS0=0;
}
int read_ADC_channel(unsigned int channel_number)
{
int value;
switch(channel_number)
{
case 0:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=0;
case 1:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=1;
default:;
}
//start AD conversion
ADGO=1;
//wait for conversion to finish
while(ADGO)
{};
//read the values in the registers
value=( ADRESH<<8 )+ADRESL;
return(value);
}
void initialize_timer1(void)
{
//The following SFR are to be set as given in the Timer1 Chapter of PIC datasheet
//set prescalar value of 1:8
T1CKPS1=1;
T1CKPS0=1;
T1OSCEN=1; //Timer1 oscillator is enabled
//set internal clock i.e. FOSC/4..eg. 20MHz osc so clk for TMR1 is 5MHZ or 200ns per TMR1 count
TMR1CS=0;
GIE=1; //global interrupt enabled
PEIE=1; //peripheral interrupt enabled
TMR1IE=1; //enable timer1 interrupt
}
void load_timer1(int timer_value)
{
TMR1ON=0; //disable timer1 before loading the values
TMR1IF=0; //timer1 flag cleared
TMR1H=(0xFFFF-timer_value)>>8; //load timer1 high register
TMR1L=(0xFFFF-timer_value)&0xFF; //load timer1 low register
TMR1ON=1; //enable timer1
}
//the interupt service routine
void interrupt isr0(void)
{
if(TMR1IF==1)
{
//At the start of interrupt service routine, reload timer
//load_timer1(timer_value);
TMR1ON=0; //disable timer1 before loading the values
TMR1IF=0; //timer1 flag cleared
TMR1H=(0xFFFF-timer_value)>>8; //load timer1 high register
TMR1L=(0xFFFF-timer_value)&0xFF; //load timer1 low register
TMR1ON=1; //enable timer1
if(light==0)//if light was off
{
off_time_counter++; //increment off_time_counter
if(off_time_counter==250) //if off_time_counter has reached the required count
{
off_time_counter=0; //clear off_time_counter
on_time_counter=0; //clear on_time_counter
light=1; //set the light On again
}
}
else if(light==1)//if light was On
{
on_time_counter++; //increment on_time_counter
if(on_time_counter==50) //if on_time_counter has reached the required count
{
off_time_counter=0; //clear the off_time_counter
on_time_counter=0; //clear the on_time_counter
light=0; //clear the light Off
}
}
}
}
//main function
int main()
{
//Declare the variables for the main function here
int analog_value1;
initialize_IO_ports();
initialize_ADC();
initialize_timer1();
load_timer1( timer_value);
//put the infinite loop here
while(1)
{
//read the digital input and set the output
PORTD = PORTB ; //read portB and output it to portD.
//output the light according to the variable 'light'
RC0=1;
RC1=1;
RC2=0;
RC3=light;
RC4=0;
//read the analog channel and store the value in the variable;
analog_value1=read_ADC_channel(0);
//check the range of the value and set or clear the RC5 accordingly
if((analog_value1>0x004F)&&(analog_value1<0x01FF)) RC5=1;
else
RC5=0;
};
return(1);
}
//the end of AdvFamLab sample program
hi, i am using pic 16f877. and below is a program. Can anyone tell me how should i know the reference voltage as they are all hex numbers. thanks a lot
//Sample program for AdvFamLab
//Include required header files here
#include<pic1687x.h>
#include<pic.h>
//Declare global variables here
volatile int on_time_counter, off_time_counter;
volatile unsigned char light=0;
int timer_value=0xFE;
//Declare functions to be used in the program, i.e.
//void initialize_IO_ports(void)
//void initialize_ADC(void)
//int read_ADC_channel(unsigned int)
//void initialize_timer1(int)
//Put the body of all the functions
void initialize_IO_ports(void)
{
//set the digital IO ports as per requirement
TRISA = 0xFF ; //portA as input
TRISB = 0xFF ; //portB as input
TRISC = 0x00 ; //portC as output
TRISD = 0x00 ; //portD as output
ADCON1= 0x82 ; //set PortE as digital io and portA as analog, and VDD and VSS; ADC output is right-justified.
//clear the output ports at the beginning
PORTD = 0x00 ; //clear portD
PORTC = 0x00 ; //clear portC
}
void initialize_ADC(void)
{
//enable the ADC
ADON=1;
//set clock source for the ADC
ADCS1=0;
ADCS0=0;
}
int read_ADC_channel(unsigned int channel_number)
{
int value;
switch(channel_number)
{
case 0:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=0;
case 1:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=1;
default:;
}
//start AD conversion
ADGO=1;
//wait for conversion to finish
while(ADGO)
{};
//read the values in the registers
value=( ADRESH<<8 )+ADRESL;
return(value);
}
void initialize_timer1(void)
{
//The following SFR are to be set as given in the Timer1 Chapter of PIC datasheet
//set prescalar value of 1:8
T1CKPS1=1;
T1CKPS0=1;
T1OSCEN=1; //Timer1 oscillator is enabled
//set internal clock i.e. FOSC/4..eg. 20MHz osc so clk for TMR1 is 5MHZ or 200ns per TMR1 count
TMR1CS=0;
GIE=1; //global interrupt enabled
PEIE=1; //peripheral interrupt enabled
TMR1IE=1; //enable timer1 interrupt
}
void load_timer1(int timer_value)
{
TMR1ON=0; //disable timer1 before loading the values
TMR1IF=0; //timer1 flag cleared
TMR1H=(0xFFFF-timer_value)>>8; //load timer1 high register
TMR1L=(0xFFFF-timer_value)&0xFF; //load timer1 low register
TMR1ON=1; //enable timer1
}
//the interupt service routine
void interrupt isr0(void)
{
if(TMR1IF==1)
{
//At the start of interrupt service routine, reload timer
//load_timer1(timer_value);
TMR1ON=0; //disable timer1 before loading the values
TMR1IF=0; //timer1 flag cleared
TMR1H=(0xFFFF-timer_value)>>8; //load timer1 high register
TMR1L=(0xFFFF-timer_value)&0xFF; //load timer1 low register
TMR1ON=1; //enable timer1
if(light==0)//if light was off
{
off_time_counter++; //increment off_time_counter
if(off_time_counter==250) //if off_time_counter has reached the required count
{
off_time_counter=0; //clear off_time_counter
on_time_counter=0; //clear on_time_counter
light=1; //set the light On again
}
}
else if(light==1)//if light was On
{
on_time_counter++; //increment on_time_counter
if(on_time_counter==50) //if on_time_counter has reached the required count
{
off_time_counter=0; //clear the off_time_counter
on_time_counter=0; //clear the on_time_counter
light=0; //clear the light Off
}
}
}
}
//main function
int main()
{
//Declare the variables for the main function here
int analog_value1;
initialize_IO_ports();
initialize_ADC();
initialize_timer1();
load_timer1( timer_value);
//put the infinite loop here
while(1)
{
//read the digital input and set the output
PORTD = PORTB ; //read portB and output it to portD.
//output the light according to the variable 'light'
RC0=1;
RC1=1;
RC2=0;
RC3=light;
RC4=0;
//read the analog channel and store the value in the variable;
analog_value1=read_ADC_channel(0);
//check the range of the value and set or clear the RC5 accordingly
if((analog_value1>0x004F)&&(analog_value1<0x01FF)) RC5=1;
else
RC5=0;
};
return(1);
}
//the end of AdvFamLab sample program