View previous topic :: View next topic |
Author |
Message |
sahu77
Joined: 08 Sep 2011 Posts: 202
|
16f1503 to 16f676 conversion |
Posted: Sat Jul 01, 2017 7:08 am |
|
|
hi everyone
Today I'm facing problem with 16f676 adc reading.
Actually i want convert micro controller. That code works fine with 16f1503 but when i convert it 16f676, adc readind reading function not work.
16f1503.h
Code: |
#include <16F1503.h>
#device ADC=11
//#FUSES WDT //Watch Dog Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES PROTECT //Code protected from reads
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(internal=4000000,restart_wdt)
//#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C3,bits=8,stream=PORT1)
#use FIXED_IO( A_outputs=PIN_A5,PIN_A2,PIN_A1 )
#use FIXED_IO( C_outputs=PIN_C5,PIN_C3,PIN_C2,PIN_C1,PIN_C0 )
volatile unsigned int16 system_tick=0;
unsigned int16 timer_adc_value;
long ac_voltage,lcutoff,hcutoff;
static unsigned char relay_status=0,relay_flag=0,timer_short_flag=0;
volatile unsigned char high_cutoff_flag=0,timer_led1_toggle_flag=0;
volatile unsigned char low_cutoff_flag=0; //added
unsigned char led2_toggle_flag = 0,timer_flag=0;
long read_ac_voltage(void);
long read_timer_value(void);
void relay_voltage(void);
void timer_delay(void);
void init_peripheral(void);
void auto_adjust_relay(void);
|
16f1503.c
Code: |
#include <stablizer_1503.h>
#include <math.h>
void main()
{
init_peripheral();
/******Timer Init*******/
output_low(RLY1_TMR); output_low(High_Low_LED);output_low(TIMER_LED);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //524 ms overflow
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
delay_ms(1);
/******* End of ADC Init*******/
/******* Start of Peripherial *******/
output_high(Buzzer);output_low(TIMER_LED);output_low(RLY1_TMR);
output_low(RLY2);output_low(RLY3);output_low(RLY4);output_low(RLY5);
output_low(High_Low_LED);
system_tick = 0;
/******* End of Peripherial *******/
delay_ms(10);
relay_voltage(); output_low(Buzzer);
while(TRUE) //Master Loop
{ ///to do something
}}
/*****************************************/
//Reading A/C Voltage
/*****************************************/
long read_ac_voltage(void)
{
unsigned char i;
long adc_value;
long peak_value=0;
set_adc_channel(0);
delay_ms(1);
for(i=0;i<=40;i++)
{
adc_value = read_adc();
if(adc_value > peak_value)
{
peak_value = adc_value;
}
delay_us(500);
}
peak_value = (peak_value*0.266);
return peak_value;
}
long read_timer_value(void)
{
long adc_value;
set_adc_channel(3);
delay_ms(1);
adc_value = read_adc();
return adc_value;
}
|
Now pls guide for 16f676 !!!!!!! _________________ sahu |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sat Jul 01, 2017 8:01 am |
|
|
hmm...
this
#device ADC=11
is probably wrong, most PIC ADCs are either 8 or 10 bits....never seen an 11 bit....
You don't say what is the 'problem' so
cut a new program,ONLY setup/read ADC/display data/loop say 1Hz.
Is the new PIC pin compatible and using the same PCB or a new PCB ? could be wiring error?
sampling 40x is not needed, better to do 32 ( faster and easier )
Jay |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Sat Jul 01, 2017 8:35 am |
|
|
temtronic wrote: | hmm...
this
#device ADC=11
is probably wrong, most PIC ADCs are either 8 or 10 bits....never seen an 11 bit....
You don't say what is the 'problem' so
|
sir i do that before
Code: | /#include <16F1503.h>
//#device ADC=11
#include <16F676.h>
#device ADC=10
/*
//#FUSES WDT //Watch Dog Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES PROTECT //Code protected from reads
//#FUSES BORV25 //Brownout reset at 2.5V
//#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
**/
//#FUSES WDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal Oscillator @ 4 Mhz
//#FUSES BROWNOUT //No brownout reset
#FUSES NOMCLR //No brownout reset
#FUSES PROTECT //Code protected from reads
//#use delay(internal=4000000,restart_wdt)
//#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C3,bits=8,stream=PORT1)
#use delay(clock=4000000) // 4 Mhz Clock
#rom 0x3FF = {0x3480} |
temtronic wrote: | ]
cut a new program,ONLY setup/read ADC/display data/loop say 1Hz.
Is the new PIC pin compatible and using the same PCB or a new PCB ? could be wiring error?
|
all pin are same need only change micro controller
Quote: | sampling 40x is not needed, better to do 32 ( faster and easier ) |
not getting what want u say ? _________________ sahu |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 01, 2017 10:26 am |
|
|
Where are the following functions in your program ? They're not there.
You are missing them:
Code: |
setup_adc_ports()
setup_adc()
|
Look in the 16F1503.h file to see what parameters you can use with
those functions. |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Sat Jul 01, 2017 11:21 am |
|
|
PCM programmer wrote: | Where are the following functions in your program ? They're not there.
You are missing them:
Code: |
setup_adc_ports()
setup_adc()
|
Look in the 16F1503.h file to see what parameters you can use with
those functions. |
That's already in code.
But my mistake,
I have not post here !!
In the 16f1503 that part is:
Code: | void init_peripheral(void)
{
set_tris_a(0x08); // RA3 TMR_relay reset
set_tris_c(0x10); // RC4 reset
/*******ADC*******/
setup_comparator(NC_NC_NC_NC);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports( sAN0|sAN3| VSS_VDD); //RA4 as timer value adc
setup_comparator(NC_NC_NC_NC);
set_tris_c(0x10); // Set Pin RC4 as input, Timer reset
port_a_pullups(0x08);//pull up on port a3
port_c_pullups(0x10);//pull up on port c4
/******* End of ADC Init*******/
} |
but 16f676 has need
Code: | //port_c_pullups(0x10); |
because when choose 16f676 and complied as
Quote: | port_c_pullups(0x10); |
I get error no 12.
Undefined identifier ...port_c_pullups _________________ sahu |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sat Jul 01, 2017 11:42 am |
|
|
Because the 676, does not offer pull-ups on port C.
This is a 'read the data sheet' one. You can only use abilities the chip actually has. |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Sat Jul 01, 2017 11:45 am |
|
|
Ttelmah wrote: | Because the 676, does not offer pull-ups on port C.
This is a 'read the data sheet' one. You can only use abilities the chip actually has. |
ok thank you for information.
Pls also mention next problem. _________________ sahu |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sat Jul 01, 2017 1:55 pm |
|
|
What you are posting cannot compile.
First it has an impossible ADC number of bits. The compiler will error on this, and refuse to compile. Then it has no port setup. You then add these. However it means that code you are are actually posting has not been used on the PIC16F1503. Similarly you show 'port_c_pullups(0x10)'. Though it won't complain this won't do what you think. On the 1503, the port_c_pullups function accepts only a TRUE/FALSE value, so not giving a pull-up on C4, but on all the port C pins...
You need to start with code that works, to have any hope of porting it to another processor. |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Sat Jul 01, 2017 2:45 pm |
|
|
Ttelmah wrote: | What you are posting cannot compile.
First it has an impossible ADC number of bits. The compiler will error on this, and refuse to compile. Then it has no port setup. You then add these. However it means that code you are are actually posting has not been used on the PIC16F1503. Similarly you show 'port_c_pullups(0x10)'. Though it won't complain this won't do what you think. On the 1503, the port_c_pullups function accepts only a TRUE/FALSE value, so not giving a pull-up on C4, but on all the port C pins...
You need to start with code that works, to have any hope of porting it to another processor. |
When i compiled it with 16f676 with modifications.
Not get any error.
Also generate hex file.
But adc function not working.
That's my main problem. _________________ sahu |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 01, 2017 9:40 pm |
|
|
The ADC doesn't work because you set pin A4 to be an output pin.
The AN3 adc input is on pin A4. AN3 must be an input for the adc to work.
You made this mistake in your tris statement for PortA below:
Quote: | void init_peripheral(void)
{
set_tris_a([b]0x08);[/b] // RA3 TMR_relay reset
set_tris_c(0x10); // RC4 reset |
You also set the Tris for pin A0 to be an output. So your other adc pin
AN0 also won't work.
My advice is, unless you understand exactly how to do it, don't bother
setting the Tris. Let the compiler do it automatically. Also, don't bother
using Fixed I/O statements. When you're a beginner, you will make
less mistakes if you let the compiler automatically handle setting the i/o
direction. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sun Jul 02, 2017 1:23 am |
|
|
As a comment, he is also ignoring the compiler warnings. If it ever worked on the 1503, it was a fluke:
">>> Warning 227 "maintest15.c" Line 2(9,15): Feature not supported ADC does not have that many bits 10<11"
For every compiler I have tried.
While warnings are sometimes things that may be ignored, when they are telling you that the chip can't do something, it becomes important to listen.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Jul 02, 2017 7:01 am |
|
|
this....
#rom 0x3FF = {0x3480}
has me 'concerned'
I'd like to know why you've stored 0x3480 in the middle of the EEPROM address space.
As for the 'read ADC 32 times not 40', the compiler will make a very,very FAST average( simple shift) as opposed to a long, time consuming /40.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sun Jul 02, 2017 7:11 am |
|
|
He is not actually averaging. He takes the peak reading. Agree wholeheartedly for an average, but for the peak doesn't matter. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Jul 02, 2017 7:18 am |
|
|
oops ! I'm so used to guys reading ADC a zillion times then averaging oddball numbers.....guess I should put the bifocals on when reading..sigh, getting old is 'fun'....
still curious about the value stuffed in the middle of ROM though...
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Sun Jul 02, 2017 8:15 am |
|
|
Agreed. Very much so. |
|
|
|