![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
mojo
Joined: 26 Sep 2006 Posts: 2
|
kbhit problem getting value into main |
Posted: Tue Sep 26, 2006 3:02 pm |
|
|
I am usually a labview programmer (i like the graphica programming better ) so I may be doing something really stupid.
I am trying to use the kbhit to determine if a char has been received on the serial port. If one has not, then it should timeout after 5 sec and continue to status 2.
If a char is received and it is A, then the program should go to status 1.
But what I am seeing now if that it will always timeout and will then go to status 1 (which it is defined as at the begining of the program, and is not changed like it should to status 2)
Any help would be appreciated.
Code: | #include "C:\Bradley\solar light dimmer\ld81505.h"
#include <input.c>
#include <math.h>
// The following initializes the first 4 locations of the data EERPOM
// using the #ROM directive
#IF defined (__PCM__)
#rom 0x2100={1,2,3,4} //initalizes ROM by inserting 1,2,3,4
#elif defined(__PCH__)
#rom int 0xf00000={1,2,3,4}
#endif
int16 photo,saved;
char answer, quit;
float photo2,on_time2,off_time2,saved2,value2,voltage;
float valuea=0,valueb=0,valuec=0,valued=0,valuee=0;
float valuef=0,valueg=0,valueh=0,valuei=0;
int timer1_flag=0,intcounter=0,light_toggle=0,zero_cross_flag=0,status=1;
long int trigger_delay=45000;
int light_set=1;
short timeout_error;
#int_ext
void zerocross_isr() {
zero_cross_flag=1;
}
#int_Timer1
Timer1_isr()
{ output_high(PIN_C0);
delay_us(600);
output_low(PIN_C0);
}
#separate
a_d_conversion(){
valuea=read_adc();
delay_us(100);
valueb=read_adc();
delay_ms(2);
valuec=read_adc();
delay_ms(2);
valued=read_adc();
delay_ms(2);
valuee=read_adc();
delay_ms(2);
valuef=read_adc();
delay_ms(2);
valueg=read_adc();
delay_ms(2);
valueh=read_adc();
delay_ms(2);
valuei=read_adc();
delay_ms(2);
valuea=(valueb+valuec+valued+valuee+valuef+valueg+valueh+valuei)/8;
}
char timed_getc(){
long timeout;
timeout_error=FALSE;
timeout=0;
while(!kbhit()&&(++timeout<50000))//wait 5 sec for user input
delay_us(100);
printf("\r\nTime\n\r");
delay_ms(10);
if(!kbhit())//if no character availble
status=2;
else {
timeout_error=TRUE;
//return(getc());
answer=getc();//get char
//printf("\r\nTimeout\n\r");
//delay_ms(10);
status=1;//if char is received, go to calibration mode
}
status=2;
}
void main()
{
byte i,j;
byte address=00; //initalize values
int16 value;
int on_time=133, off_time=133;
int16 m,l,k,p=0;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_Internal);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
setup_timer_1(T1_internal|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//enable_interrupts(INT_EXT);//disabled to allow use of B0
//enable_interrupts(GLOBAL);
printf("\f"); //clears screen
delay_ms(10);
printf("\n\r");
delay_ms(10);
// printf("*****\n\r");
// delay_ms(10);
printf("A=Calibrate to 400\n\r");
delay_ms(10);
//printf("B= Test Cell\n\r");
//delay_ms(10);
output_high(PIN_C0);//turns light on at full power
delay_ms(6000);
output_low(PIN_C0);
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(TRUE)
{
switch (status)
{
case (1):
if (answer = 'A')//if user pressed A, read voltage off cell
{
set_adc_channel(1);//channel RA1 for cal photo cell
delay_cycles(60);
address = 00;
value = read_adc(); //reads the adc at a set lux level
delay_ms(4);
write_eeprom(address, make8(value,0)); //enter first 8 bits
write_eeprom(address+1, make8(value,1)); //enter next 8 bits in next memory location
printf("\r\n*****\n\r");
delay_ms(10);
//printf("Bytes = %lu\n\r", value);//for debugging
//delay_ms(100);
photo2 = ((float)value/1024)*5;//conversion
printf("Voltage = %1.3f\n\r", photo2);//prints the voltage in float
printf("Calibration\n\r");
delay_ms(5);
printf("complete\n\r");
for(p=0;p<11;p++)//allow user to switch power plug
{delay_ms(1000);
printf("\r\n%lx\n\r",p);
}
status=2;//go to reading voltage off cal cell
}
else{
status=2;//if char not = A, go to setting light level
}
break;
case (2): *(&saved) = read_eeprom(address); //gets lsb
*(&saved+1) = read_eeprom(address+1); //gets msb
set_adc_channel(1);
saved2=((float)saved/1024)*5;//convert save hex to voltage
printf("\r\nsaved=%f\n\r",saved2);
status=3;//go to controlling light and reading voltage
break;
case (3): if(zero_cross_flag==1){//wait for zero cross
//intcounter++;
set_timer1(trigger_delay); //initially 44688 (4.16mS)
SET_ADC_CHANNEL(1);
delay_cycles(30); // wait
valuea=read_adc(); //get value
//delay_us(100);
voltage=((float)valuea/1024)*5;
// printf("\r\n%f\n\r",valuea);
if((voltage<=(saved2-0.02))&& (trigger_delay<62000>=(saved2+0.02))&&(trigger_delay>=24000))
trigger_delay=trigger_delay-3;
if((voltage>=(saved2-0.005))&&(voltage<=(saved2+0.005)))
{light_toggle=0;//indicates light level is correct
if(light_set!=0)
{printf("\r\n%f\n\r",voltage);
light_set=0;
}
else
light_set=0;
}
else
light_toggle=1;
delay_cycles(60); // wait
//printf("\r\nt=%lu\n\r",trigger_delay);
//printf("\r\nv=%f\n\r",voltage);
zero_cross_flag=0;
break;}break;}
}
}
reset_cpu();
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 26, 2006 3:51 pm |
|
|
Quote: |
I am trying to use the kbhit to determine if a char has been received on
the serial port. If one has not, then it should timeout after 5 sec and
continue to status 2.
But what I am seeing now if that it will always timeout.
|
In the posted code, the timed_getc() function is never called.
Also, you don't show your #use rs232() statement, and other CCS
directives (#fuses, etc.). |
|
![](templates/subSilver/images/spacer.gif) |
mojo
Joined: 26 Sep 2006 Posts: 2
|
|
Posted: Wed Sep 27, 2006 6:33 am |
|
|
Here is my .h file
[code]#include <16F877A.h>
#device *=16
#device adc=10
#org 0x1F00, 0x1FFF void loader16F876(void) {} //protect bootloader code for the 8k 16F876/7
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use fixed_io(c_outputs=Pin_C0, PIN_C1)
In order to call the timed_getc(), should i use a different vairble say b (which is a char) and use b = timed_getc() in the main do an if else, if b = 'A' go to status 1 and else go to status 2??
Thanks for your help. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|