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

Serial communication on 12F509
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Jun 25, 2022 10:02 am     Reply with quote

How dare you use such language on the site!... Smile
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sat Jun 25, 2022 1:51 pm     Reply with quote

Thank you for reply.
This is real project, test with scope for input/output for confirmation where is the hardware problem.
I use USB serial and is working in PUTTY when is RX-TX in short.
Normaly I must receive from chip 0X01 and 0X10 and infinitly 0X10.
I use FT232R chip, so pinout is correctly OKY.
But why not even simply blinking led is not working.
Also A0 where I have LED with 470R is OFF.
code is:

Code:
#include <16F1826_uart.h>

void main()
{
setup_adc_ports(sAN1, VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_oscillator (OSC_8MHZ | OSC_PLL_on);

putc(0X01);  ///<<<<<<<< there is the problem/bloking the program
output_low(LED);
while(TRUE)
{
putc(0X10);

if(kbhit()){
   MEM_SERIAL = getc();
   }

if (MEM_SERIAL==100){
   output_toggle(PIN_A7);
   }

output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);

}

}


and

Code:
#include <16F1826.h>
#device ADC=8

#FUSES MCLR 
#FUSES NOBROWNOUT
#FUSES IESO
#FUSES FCMEN
#FUSES NOWRT
#FUSES STVREN
#FUSES BORV19
#FUSES LVP

#use delay(internal=32MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1)

#define LED PIN_A0
#define DELAY 1000

static unsigned int8 MEM_SERIAL;


so where is the problem?
temtronic



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

View user's profile Send private message

PostPosted: Sat Jun 25, 2022 5:18 pm     Reply with quote

this ...
#FUSES LVP

change to NOLVP
as very, very few programming devices are actually 'low voltages'.

Also add 'ERRORS' to the Use rs232 (...options...),actually always add it !

and disable the comparators , pretty sure the default is 'enabled'..

Found that RX, TX are remappable using APFCONx pins ! Need to check datasheet to see what they default to be...

hmm, you Do have a pullup resistor on MCLR pin ?

It'd be a lot easier to see the whole program as ONE program. Instead of the xxxuart.h header and 'main', combine into ONE program.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Jun 25, 2022 10:27 pm     Reply with quote

and also don't use ADC_CLOCK_INTERNAL
Doesn't matter for the serial, but on 99% of PIC's (all except some of
the very newest chips and the DSPIC's), the internal RC clock should
only be used by stopping the processor when the reading is taken.
Otherwise accuracy plummets.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 1:49 am     Reply with quote

Thank you for reply.
I made adjustment to program as your replied but still not working.

Code:
#include <16F1826_uart.h>

void main()
{
//setup_adc_ports(sAN1, VSS_VDD);
//setup_adc(ADC_CLOCK_INTERNAL);
setup_oscillator (OSC_8MHZ | OSC_PLL_on);
setup_comparator (NC_NC_NC_NC);

BIT_SET(RXDTSEL,0); //RX to RB1
BIT_SET(TXCKSEL,0); //TX to RB2

//output_low(LED);   //<<<<< when is enabled outpus is at 0V
putc(0X01);          //<<<<< there is the problem ?!?!?
output_low(LED);

while(TRUE)
{
putc(0X10);

if(kbhit()){
   MEM_SERIAL = getc();
   }

if (MEM_SERIAL==100){
   output_toggle(PIN_A7);
   }

output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);

}

}


and
Code:

#include <16F1826.h>
#device ADC=8

#FUSES NOMCLR               
#FUSES NOBROWNOUT   
#FUSES IESO 
#FUSES FCMEN 
#FUSES NOWRT       
#FUSES STVREN   
//#FUSES BORV19   
#FUSES NOLVP           

#use delay(internal=32MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1,ERRORS)

#byte APFCON0 = getenv("SFR:APFCON0")
#bit RXDTSEL = APFCON0.7
#byte APFCON1 = getenv("SFR:APFCON1")
#bit TXCKSEL = APFCON1.0

#define LED PIN_A0
#define DELAY 1000

static unsigned int8 MEM_SERIAL;
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 2:29 am     Reply with quote

oh, I changed to correct one.

Code:
BIT_clear(RXDTSEL,0); //RX to RB1
BIT_clear(TXCKSEL,0); //TX to RB2

But still not working

Also

Code:
//output_low(LED);   //<<<<< when is enabled outpus is at 0V


This confirm me that CPU is working, but after this line is like blocking.
temtronic



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

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 4:52 am     Reply with quote

really need to do the basic '1hz LED' test.
Just have main() , toggle an LED on odd at 1Hz

use this of your code...

output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);

where DELAY is 500.

forget about 'serial' parts of the program.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 4:55 am     Reply with quote

Quote:
output_low(LED); // <<<<< when is enabled output is at 0V

This confirm me that CPU is working, but after this line is like blocking.

Do you have a series resistor for your LED ?

If you don't have a series resistor, the PIC will probably shut down when
you turn on the LED.

You could use 220 ohms.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 6:02 am     Reply with quote

About of resistor, I use 470ohm.

this code is working blinking once /second.

Code:
#include <16F1826_uart.h>

void main()
{
//setup_adc_ports(sAN1, VSS_VDD);
//setup_adc(ADC_CLOCK_INTERNAL);
setup_oscillator (OSC_8MHZ | OSC_PLL_on);
setup_comparator (NC_NC_NC_NC);

BIT_clear(RXDTSEL,0); //RX to RB1
BIT_clear(TXCKSEL,0); //TX to RB2

//output_low(LED);   //<<<<< when is enabled outpus is at 0V
//putc(0X01);          //<<<<< there is the problem ?!?!?
output_low(LED);

while(TRUE)
{
//putc(0X10);

//if(kbhit()){
//   MEM_SERIAL = getc();
//   }

//if (MEM_SERIAL==100){
//   output_toggle(PIN_A7);
//   }

output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);

}

}


#include <16F1826.h>
#device ADC=8

#FUSES NOMCLR   
#FUSES NOBROWNOUT   
//#FUSES IESO     
//#FUSES FCMEN 
#FUSES NOWRT   
//#FUSES STVREN 
//#FUSES BORV19 
#FUSES NOLVP         

#use delay(internal=32MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1,ERRORS)

#byte APFCON0 = getenv("SFR:APFCON0")
#bit RXDTSEL = APFCON0.7
#byte APFCON1 = getenv("SFR:APFCON1")
#bit TXCKSEL = APFCON1.0

#define LED PIN_A0
#define DELAY 500

//static unsigned int8 MEM_SERIAL;
temtronic



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

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 6:54 am     Reply with quote

great so the PIC does work !
I've rearranged your code as one file, makes it a lot easier for me to read and 'flows' from the top down....silly me can't get it 'green' with code[] button.

Code:

#include <16F1826.h>
#device ADC=8

#FUSES NOMCLR   
#FUSES NOBROWNOUT   
//#FUSES IESO     
//#FUSES FCMEN
#FUSES NOWRT   
//#FUSES STVREN
//#FUSES BORV19
#FUSES NOLVP         

#use delay(internal=32MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1,ERRORS)

#byte APFCON0 = getenv("SFR:APFCON0")
#bit RXDTSEL = APFCON0.7
#byte APFCON1 = getenv("SFR:APFCON1")
#bit TXCKSEL = APFCON1.0

#define LED PIN_A0
#define DELAY 500

//static unsigned int8 MEM_SERIAL;
void main()
{
   //setup_adc_ports(sAN1, VSS_VDD);
   //setup_adc(ADC_CLOCK_INTERNAL);
   setup_oscillator (OSC_8MHZ | OSC_PLL_on);
   setup_comparator (NC_NC_NC_NC);

   BIT_clear(RXDTSEL,0); //RX to RB1
   BIT_clear(TXCKSEL,0); //TX to RB2

   //output_low(LED);   //<<<<< when is enabled outpus is at 0V
   //putc(0X01);          //<<<<< there is the problem ?!?!?
   output_low(LED);

   while(TRUE)
   {
       //putc(0X10);

       //if(kbhit()){
       //   MEM_SERIAL = getc();
       //   }

       //if (MEM_SERIAL==100){
       //   output_toggle(PIN_A7);
       //   }

       output_low(LED);
       delay_ms(DELAY);
       output_high(LED);
       delay_ms(DELAY);

    }

}


one possible problem is you have use delay set for 32MHz then later setup osc at 8MHz/pll on. I don't think you need the setup osc command.

I prefer to delete lines instead of commenting them out, again it makes it 'cleaner' to look at.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 7:21 am     Reply with quote

As a comment, don't use all capitals for variables.

The 'standard' in C, is to reserve ALL CAPITALS for #defines. Then used
mixed case for variables. So Mem_Serial.
The advantage of this is when looking through your program and you
see VALUE, you then 'know' that VALUE is a macro. It is a very useful
little 'immediate bit of information'.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 8:24 am     Reply with quote

I clean up much as possible so now, I modify the code for easy read/understand what is happening.

Code:
#include <16F1826_uart.h>

void main()
{

setup_comparator (NC_NC_NC_NC);

BIT_clear(RXDTSEL,0); //RX to RB1
BIT_clear(TXCKSEL,0); //TX to RB2

while(TRUE)
{

//output_low(LED);   //
putc(0X01);          //<<<<< there is the problem ?!?!?

output_low(LED);
delay_ms(DELAY);
output_high(LED);
delay_ms(DELAY);

}


and
Code:


#include <16F1826.h>
#device ADC=8

#FUSES NOMCLR                   //Master Clear pin enabled
#FUSES NOBROWNOUT               //No brownout reset
//#FUSES IESO                     //Internal External Switch Over mode enabled
//#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWRT                    //Program memory not write protected
//#FUSES STVREN                   //Stack full/underflow will cause reset
//#FUSES BORV19                   //Brownout reset at 1.9V
#FUSES NOLVP                    //Low Voltage Programming on B3(PIC16) or B5(PIC18)

#use delay(internal=8MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=PORT1,ERRORS)

#byte APFCON0 = getenv("SFR:APFCON0")
#bit RXDTSEL = APFCON0.7
#byte APFCON1 = getenv("SFR:APFCON1")
#bit TXCKSEL = APFCON1.0

#define LED PIN_A0
#define DELAY 500


why this is not working?
putc(0X01);
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 8:55 am     Reply with quote

Almost certainly you are sending the wrong thing.

Do you really want to send the code 1?. In RS232 comms this is the code
for Start of header.
I suspect you actually want to send '1' (the ASCII code for the 'digit' 1.
So

putc('1');

Using hex this is 0x31.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 8:58 am     Reply with quote

question:
If this pin TX (B2) is okay I must have 0V (or some digital oscillation 0-5V).
Why on this pin I have tristate ? voltage between 0-5V. When I put my hand I have 4V not steady value. So output is not activated for UART...


Last edited by nailuy on Sun Jun 26, 2022 9:15 am; edited 1 time in total
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Sun Jun 26, 2022 9:05 am     Reply with quote

Ttelmah wrote:
Almost certainly you are sending the wrong thing.

Do you really want to send the code 1?. In RS232 comms this is the code
for Start of header.
I suspect you actually want to send '1' (the ASCII code for the 'digit' 1.
So

putc('1');

Using hex this is 0x31.


putc(0X01); is for testing
I can change to putc(100);
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, 3  Next
Page 2 of 3

 
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