View previous topic :: View next topic |
Author |
Message |
PrinceNai
Joined: 31 Oct 2016 Posts: 463 Location: Montenegro
|
|
Posted: Fri Jun 10, 2022 10:59 am |
|
|
I took the liberty to simplify your code a bit, namely testing the u_off part. It executes every time '1' is received, so you can handle that directly there when you get that '1'.
Code: |
void main(void)
{
...
int32 loop_usb = 0;
// char u_off = 10; ?
while(true)
{
if (command != 0)
{
//Only execute the outputs if a new command arrives. Is this working? Do you come here? toggle a2 here to test if you receive commands.
if(command == '0')
{
output_high(PIN_A0); // is this part working?
}
else if(command == '1')
// I'd hook a diode here and turn it on and off to be sure I ever come here. For testing purposes PIN_A2 is used
{
output_toggle(PIN_A2); // change the state of the diode every time you receive an "1". Debugging purpose only, to see if you even come here.
// u_off = 0; // not needed any more, you can do it all here if you have command "1"
if(loop_usb == 65535)
{
// output_high(PIN_A2);
loop_usb = 0;
}
else if(loop_usb == 131070)
{
// output_low(PIN_A2);
loop_usb = 0;
}
}
command = 0; // reset for new command
}
}
}
|
Last edited by PrinceNai on Fri Jun 10, 2022 12:00 pm; edited 4 times in total |
|
![](templates/subSilver/images/spacer.gif) |
PrinceNai
Joined: 31 Oct 2016 Posts: 463 Location: Montenegro
|
|
Posted: Fri Jun 10, 2022 11:29 am |
|
|
The idea is to "see" how far down the tests you get and where it stops (if it does). Start with the leftmost "if" and go down the line with toggling the diode. |
|
![](templates/subSilver/images/spacer.gif) |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Fri Jun 10, 2022 3:44 pm |
|
|
This is the whole code.
Code: | #include <12F683.h>
//#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPROTECT,NOMCLR
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
//#FUSES CPD //No EE protection
//#FUSES PROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin enabled
//#FUSES NOPUT //No Power Up Timer
//#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES BROWNOUT //Brownout reset enabled
//#byte OSCTUNE = 0x90
//#use delay(clock=4000000)
//#use rs232(baud=9600, rcv=PIN_A3)
#use delay(clock=8000000)
#use rs232(baud=9600, rcv=PIN_A3, SAMPLE_EARLY)
#use fast_io(a)
char command=0; //start with command empty
int32 loop_usb=0;
char u_off;
void resetPorts(void) ;
//===============================
#INT_RA
void int_ra_isr(void)
{
if(input_state(pin_A3)==0) //it only works with this command.
{
command = getc();
}
}
//===============================
void resetPorts(void)
{
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A4);
output_low(PIN_A5);
}//-----------
//-----------------------------------------------------------------------------
void main(void)
{
// OSCTUNE = 0x01;
// setup_oscillator(OSC_4MHZ);
setup_oscillator(OSC_8MHZ);
setup_comparator( NC_NC_NC_NC ); // disable comparators
setup_adc_ports(NO_ANALOGS); // disable analog inputs
setup_adc(ADC_OFF); // disable A2D
enable_interrupts(INT_RA3);
enable_interrupts(GLOBAL);
set_tris_a(0b00001000);
resetPorts();
u_off = 10;
while(true)
{
if (command!=0)
{
//Only execute the outputs if a new command arrives
if(command=='0')
{
output_high(PIN_A0);
}
if(command=='1')
{
output_low(PIN_A0); //reset = 0
}
//............................
if(command=='2')
{
output_high(PIN_A1);
}
if(command=='3')
{
output_low(PIN_A1); //reset = 0
}
//............................
if(command=='4')
{
output_high(PIN_A2); //USB OK = 1
}
if(command=='5')
{
output_low(PIN_A2); //USb OFF = 0
u_off = 0;
}
//.............................
if(command=='6')
{
output_high(PIN_A4);
}
if(command=='7')
{
output_low(PIN_A4); //reset = 0
}
//.............................
if(command=='8') //aviso tempo < 10 sec (duração 1 segundo)buzzer
{
output_high(PIN_A5);
delay_ms(500);
output_low(PIN_A5);
}
if(command=='9') //reset = 0
{
output_low(PIN_A5);
}
//.............................
if(command=='s')
{
output_high(PIN_A5); //aviso tempo < 10 sec (duração 1 segundo)buzzer
}
//------------------------------
if(command=='r') //reset (saidas = 0)
{
resetPorts();
}
//........................
if(command=='f') //usb off - stand alone
{
u_off = 0;
}
//........................
command=0; //clear the command byte, so output stops till
//a new command arrives.
}
//========== while end ==========
loop_usb++;
if(u_off == 0)
{
if(loop_usb == 65535)
{
output_high(PIN_A2);
}
if(loop_usb == 131070)
{
output_low(PIN_A2);
loop_usb = 0;
}
}
}
} |
Everything works, but the u_off variable only works if it is initialized. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Fri Jun 10, 2022 4:15 pm |
|
|
OK....
subject line...
12F675 software UART
1st line of latest program...
#include <12F683.h>
..so now I'm confused....
Jay |
|
![](templates/subSilver/images/spacer.gif) |
vtrx
Joined: 11 Oct 2017 Posts: 142
|
|
Posted: Fri Jun 10, 2022 4:34 pm |
|
|
Sorry for the confusion, I decided to use the cheaper one.
I did some tests here and it worked like this:
Code: |
...
//========== while ==========
// loop_usb++;
if(u_off == 0)
{
loop_usb++;
if(loop_usb == 65535)
{
output_high(PIN_A2);
}
if(loop_usb == 131070)
{
output_low(PIN_A2);
loop_usb = 0;
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Sat Jun 11, 2022 1:16 am |
|
|
"the u_off variable only works if it is initialized."
What do you think u_off will contain if it is 'not initialised'?.
Generally RAM contents are _undefined_ on boot. It could contain
anything.
1, 75456, 23567891, etc. etc..
With an int32, could be anything up to 4294967295.
You can never assume a variable 'starts' with a value, unless you either
initialise it, or use #zero_ram.
Add one qualifier to that statement. 'Unless it is declared as static'. A
static variable by default is always initialised to zero, unless you declare
something else. |
|
![](templates/subSilver/images/spacer.gif) |
|