View previous topic :: View next topic |
Author |
Message |
ralph79
Joined: 29 Aug 2007 Posts: 87
|
1824T39A |
Posted: Mon Feb 04, 2019 9:34 am |
|
|
Hi all,
I'm working with the 16LF1824T39A, with the CCS 5.078, but I can't put it working with internal uart (tx buffer) PIN_C4 (PIN_B4 in the include file), it gives always "Error#96 Baud rate out of range" for an baud rate of 57600 @ 4Mhz internal clock. Is there any workaround to accomplish that? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Feb 04, 2019 9:54 am |
|
|
Work out what the baud rate generator is trying to do.
Calculate the baud rate generator divisor by dividing your clock frequency first by 16 then your intended baud rate. Now round off to the nearest whole number. How much error does the rounding off create? It needs to be of the order of a few percent.
Mike |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Mon Feb 04, 2019 10:36 am |
|
|
Hi Mike,
I need a standard baud rate, the reason for this is that I have an another micro (in this case is the 16F1825) that will receive the data by int_rda interrupt. That is the reason for trying to have a fast baud rate but standard (at least I need 19200bps...)
Digging both datasheets I can put different clock speeds (in this case 3.6864Mhz or 18.432 Mhz) with a much lower rate error..but using the normal functions of CCS Code: | #use delay(internal=xxMhz) | is not possible...
Furthermore, I don´t know if in fact the Code: | #use rs232(baud = 57600, parity=N, xmit=PIN_A0, rcv=PIN_A1, bits=8, errors, RESTART_WDT) | , is sending the data by software uart or by hardware uart... |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Mon Feb 04, 2019 12:01 pm |
|
|
It is a software uart, I believe. This chip has hardware uart on pins C4 and C5. You say that you need at least 19200 baud rate. With your setup even 38400 compiles fine. If you change clock speed to 8MHz, 5.600 works too. |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Mon Feb 04, 2019 12:25 pm |
|
|
the chip has an TX on PIN_A0 or in PIN_C4, according to device datasheet page 4:
"Pin function is selectable via the APFCON0 or APFCON1 registers."
So in this case I have done the following:
Code: | #byte APFCON0 = getenv("SFR:APFCON0") |
and in the init function I put the following
Code: |
setup_uart(FALSE, PORT1);
APFCON0 |= 0x04;
setup_uart(TRUE, PORT1);
|
The problem is that I can't guarantee that even like the code shown above it is working by HW or SW ... if it is working by HW, I should have clock speed (4MHZ) to have the baud rate of 57600bps... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Feb 04, 2019 12:41 pm |
|
|
You can always ensure the hardware UART is used by using:
#use rs232(baud = 57600, parity=N, UART1, bits=8, errors)
This forces the code to be generated for hardware UART1.
Then simply select the pins with the APFCON register. |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Sat Feb 23, 2019 7:54 am |
|
|
Sorry for my delay, but I thought I had respond to this post, but for any reason, it was not published.
I had make as you have suggested (use the UART1) in the #use rs232 statment and it works just fine thanks for that... |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Sat Feb 23, 2019 8:22 am |
|
|
now I have a slightly different problem that is making me nuts.. it should be simple but, somehow it misses something...
I have two analog inputs one for reading battery level (monitor) and the other for an normal ad input. So my code is as follows:
.....
Code: | #define normal_ad_entry PIN_C3
#define Low_power_Reading PIN_C2
#define ent_adc_normal_ad sAN7
#define ent_adc_Low_power sAN6
#define adc_ch_normal_ad 7
#define adc_ch_Low_power 6 |
---- code for reading the ADs inputs: ---
Code: | setup_adc(ADC_OFF);
delay_ms(10);
setup_adc_ports(NO_ANALOGS);
set_analog_pins(Low_power_Reading);
delay_ms(10);
setup_vref(VREF_ON | VREF_ADC_1v024);
setup_adc_ports(ent_adc_Low_power, VSS_FVR);
delay_us(10);
setup_adc(ADC_CLOCK_INTERNAL);
delay_us(10);
set_adc_channel(adc_ch_Low_power); // also tried wit FVR_CHANNEL);
delay_us(10);
//read_adc(ADC_START_ONLY);
//while(!adc_done());
//read_adc(ADC_READ_ONLY);
battery_adc_value_is = read_adc(); // and I don't have the correct values I was expecting to have... |
for the other input which works just fine is as follows:
Code: | setup_adc_ports(NO_ANALOGS);
set_analog_pins(normal_ad_entry);
delay_ms(10);
setup_vref(VSS_VDD);
delay_us(10);
setup_adc_ports(ent_adc_normal_ad, VSS_VDD);
delay_us(10);
setup_adc(ADC_CLOCK_INTERNAL);
delay_us(10);
set_adc_channel(adc_ch_normal_ad);
delay_us(10);
//read_adc(ADC_START_ONLY);
//while(!adc_done());
//read_adc(ADC_READ_ONLY);
value_from_normal_ad_input = read_adc(); // and I don't have the correct values |
and I just can't understand why... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Feb 23, 2019 8:50 am |
|
|
There are several basic issues with what you are doing. First your ADC clock.
From the data sheet:
Quote: |
When the device frequency is greater than 1 MHz, the FRC clock source is only recommended if the
conversion will be performed during Sleep.
|
Your clock is not recommended.
Then your use of the FVR.
It is not legal to use the 1.024V output from the FVR. The 2.048V output
is the lowest one legal for the ADC.
If you look at the selections in the data sheet:
Quote: |
The ADPREF bits of the ADCON1 register provides
control of the positive voltage reference. The positive
voltage reference can be:
• VREF+ pin
• VDD
• FVR 2.028V
• FVR 4.096V (Not available on LF devices)
|
Note the lack of the 1.024V selection.
The reason is that this is less than the ADC is rated to run with. The
minimum Vref the ADC is rated to use is 1.8V.
Now you don't say what the voltage you are trying to measure actually
'is'. However it must not be greater than the Vref.
Change your clock to use FOSC/8 (the recommended setting for 4MHz),
and select the 2.048 Vref, and you may well find things start to work. |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Sat Feb 23, 2019 9:18 am |
|
|
I was trying to use the FVR module
14.0 FIXED VOLTAGE REFERENCE (FVR)The Fixed Voltage Reference (FVR), Quote: | is a stable voltage reference, independent of VDD, with 1.024V, 2.048V or4.096V selectable output levels. The output of the FVR can be configured to supply a reference voltage to the following:
• ADC input channel
• ADC positive reference
• Comparator positive input
• Digital-to-Analog Converter (DAC)
The FVR can be enabled by setting the FVREN bit ofthe FVRCON register |
About the clock I had already use tested with the Fosc/8 and Fosc/4 with no luck, that was why I had changed to the internal_adc_clock..
About the voltage, I'm measuring, the micro is powered my and CR2032 battery and I have an external circuit that (from my calculations) could be from 0 to 6V (maximum). In this case as I have only 3.3V, I have to make the corresponding calculations, but in fact the problem was the readings that were not correct.. I will test it again...in a short time I will let you know |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Feb 23, 2019 9:32 am |
|
|
According to the PIC's datasheet, ADC electrical section...
AD07 V AIN Full-Scale Range VSS — VREF V
Since you're using FVR of 2.028 as the VREF, the INPUT to the ADC cannot be greater than 2.028
So the way I read the spec, you can't read the 6 volt input !! It's NOT allowed.
To read a voltage greater than VREF, you can use a voltage divider using 2 , 1% resistors, say 2K and 1K, that will make the 6 volt source look like 2 volts. For accurate measurements, run a few 'calibrations tests' to get the correct 'multiplier' when displaying the '6v reading'.
Jay |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Sat Feb 23, 2019 9:34 am |
|
|
welll... with your suggestions.. now is working (the value that I half of the one I was expecting as it supposed)... so my datasheet reading was not as accurate as it should...thanks Ttelmah |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Sat Feb 23, 2019 9:36 am |
|
|
temtronic, I don't read the 6V... I'm just saying that my external circuit can(or could) handle the 6V and treat it as 3.0V in the micro pin input. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Feb 23, 2019 11:09 am |
|
|
3v, is still above the Vref. So you need to think again, and redesign
the divider to ensure the voltage can never go above your Vref. |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
|
Posted: Mon Feb 25, 2019 6:15 am |
|
|
below I send my ancient circuit:
VCC is the battery voltage, is the same voltage for the micro power supply.
[img] https://imgur.com/a/WfwcVkZ [/img]
The ACT_L_POWER is an dedicated output pin (active low), so my idea was, when I want to read the battery voltage, make an active low to the ACT_L_POWER pin, and make the ADC reading based on the voltage reference. I would like to use the 1.024V because the micro can be operating at 1.8V...if that isn't possible I will have to use an external voltage reference... |
|
|
|