View previous topic :: View next topic |
Author |
Message |
weland98
Joined: 20 Mar 2018 Posts: 3 Location: Colombia
|
Interrupts and operations |
Posted: Tue Mar 20, 2018 4:33 pm |
|
|
Good day,
I am trying to write a code in order to achieve that with the interrupts the analog inputs refresh each 10ms.
Also to take a sin signal for one analog input, and 5v variable with a potentiometer.
The code compiles but, when I try to simulate, it won't show any signal on the oscilloscope.
Code: |
#include <16f877a.h>
#device adc=10
#fuses hs,nowdt,noprotect,noput
#use delay (clock=4M)
#include <lcd.c>
#define lcd_data_port getenv("sfr:portb")
#int_timer0
#define lcd_rs_pin pin_b0
#define lcd_rw_pin pin_b1
#define lcd_enable_pin pin_b2
void timer_0()
{
output_toggle(pin_b0);
}
void main ()
{
setup_timer_0(rtcc_internal|rtcc_div_2);
set_timer0(6);
enable_interrupts(int_timer0);
enable_interrupts(global);
int16 q;
int16 j;
float p;
float r;
float m;
float k=0.5;
setup_adc_ports(an0_an1_an3);
setup_adc(adc_clock_internal);
lcd_init();
while(true)
{
set_adc_channel(0); q=read_adc();
set_adc_channel(1); j=read_adc();
delay_us(20);
lcd_gotoxy(1,1);
p=(5.0*q)/1024.0;
r=(5.0*j)/1024.0;
m=(k*p)+r;
printf(lcd_putc,"ADC = %4Ld",j);
printf(lcd_putc,"\n conve = %1.2f",m);
}
}
|
Thanks in advance if you can help me to spot, whats wrong with the code, or if I missing something. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Tue Mar 20, 2018 7:23 pm |
|
|
I copied/pasted/compiled using PCM and got 7 fatal errors which did not include the wrong adc_clock error( see adc section of the datasheet as to why).
I can only assume when you say 'compiles' you're referring to the Proteus ISIS simulated compilation and not CCS PCM.
Also you cannot apply a 'sin signal' to the ADC of that PIC ! 'sin' implies a sinusoidal or alternating signal going from some positive value through zero to some negative value aka a sinewave, like 50Hz power. Again, please check the PIC's datasheet, electrical specifications, adc subsection as to why.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 20, 2018 8:06 pm |
|
|
At a minimum, you need to re-arrange the lines of code in your program
to be in this order (for each section):
Code: |
#define lcd_data_port getenv("sfr:portb")
#define lcd_rs_pin pin_b0
#define lcd_rw_pin pin_b1
#define lcd_enable_pin pin_b2
#include <lcd.c>
#int_timer0
void timer_0()
{
output_toggle(pin_b0);
}
|
Code: |
set_adc_channel(0);
delay_us(20);
q=read_adc();
set_adc_channel(1);
delay_us(20);
j=read_adc();
|
|
|
|
weland98
Joined: 20 Mar 2018 Posts: 3 Location: Colombia
|
|
Posted: Wed Mar 21, 2018 9:54 am |
|
|
When I say it compiles,
I am refering to CCS C Compiler
Code: |
#include <16f877a.h>
#device adc=10
#fuses hs,nowdt,noprotect,noput
#use delay (clock=4M)
#int_timer0
#DEFINE PIN_A3 m
void timer_0()
{
output_toggle(pin_b0);
}
void main ()
{
setup_timer_0(rtcc_internal|rtcc_div_2);
set_timer0(100);
enable_interrupts(int_timer0);
enable_interrupts(global);
int16 q;
int16 j;
float p;
float r;
float m;
float k=0.5;
setup_adc_ports(an0_an1_an3);
setup_adc(adc_clock_internal);
while(true)
{
set_adc_channel(0);
delay_us(20);
q=read_adc();
set_adc_channel(1);
delay_us(20);
j=read_adc();
//p=(5.0*q)/1024.0;
//r=(5.0*j)/1024.0;
m=(k*j*q);
output_toggle(PIN_B4);
}
}
|
I re-arranged the code to this, the tutor basically said it wasn't problem putting a sinewave, but I have to do the digital-analog conversion, but checking the datasheet this PIC it's not supposed to have that module, although that's a must for the code, could you help me with that issue? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Mar 21, 2018 10:50 am |
|
|
this...
Quote: |
Also to take a sin signal for one analog input, |
...made me think you were trying to READ a sine wave though your last post sounds like you want the PIC to generate a sinewave.
There are several ways to have a PIC generate a sinewave however READING a sinewave will involve additional parts like opamp, resistors, etc. to 'offset' and 'scale' the AC voltage into a 0 to 5 volt level the PIC ADC can read. |
|
|
weland98
Joined: 20 Mar 2018 Posts: 3 Location: Colombia
|
|
Posted: Wed Mar 21, 2018 11:04 am |
|
|
Not actually,
What it is asked is that, with two analog inputs, (one of them a sinewave generated with a normal signal generator) and the other one a 5v pot variable. Now, it converts these two inputs to digital, it operates, and the output has to be analog again (digital-analog). But he wants to see the sinewave like if it was in discrete time with the interruptions (10ms) thus when you variate the pot, an offset can be seen on the oscilloscope. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Mar 21, 2018 11:32 am |
|
|
re... your comment of ...
Quote: | one of them a sinewave generated with a normal signal generator | ...
That PIC will NOT properly read a sinewave, it cannot read the negative portion of a sinewave. The ADC section ONLY works for positive input values from gnd to VDD ( or Vref...) Have a look at the ADC specs in the electrical specs section |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Mar 21, 2018 1:05 pm |
|
|
and (of course) the input will be distorted as the signal hits the clamp diodes.... |
|
|
|