CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

12F675 software UART
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 10:59 am     Reply with quote

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
PrinceNai



Joined: 31 Oct 2016
Posts: 452
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 11:29 am     Reply with quote

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.
vtrx



Joined: 11 Oct 2017
Posts: 141

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 3:44 pm     Reply with quote

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.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 4:15 pm     Reply with quote

OK....

subject line...
12F675 software UART

1st line of latest program...
#include <12F683.h>

..so now I'm confused....
Jay
vtrx



Joined: 11 Oct 2017
Posts: 141

View user's profile Send private message

PostPosted: Fri Jun 10, 2022 4:34 pm     Reply with quote

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;
         }         
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Jun 11, 2022 1:16 am     Reply with quote

"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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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