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

Interrupts and operations

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
weland98



Joined: 20 Mar 2018
Posts: 3
Location: Colombia

View user's profile Send private message

Interrupts and operations
PostPosted: Tue Mar 20, 2018 4:33 pm     Reply with quote

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: 5960
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 20, 2018 7:23 pm     Reply with quote

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: 20220

View user's profile Send private message

PostPosted: Tue Mar 20, 2018 8:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 9:54 am     Reply with quote

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: 20220

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 10:21 am     Reply with quote

Sine wave using PWM and LPF:
https://www.ccsinfo.com/forum/viewtopic.php?t=26636
This is a 2 page thread.
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 10:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 11:04 am     Reply with quote

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: 5960
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 11:32 am     Reply with quote

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: 12841

View user's profile Send private message

PostPosted: Wed Mar 21, 2018 1:05 pm     Reply with quote

and (of course) the input will be distorted as the signal hits the clamp diodes....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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