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 CCS Technical Support

PIC16F616 PWM and ADC

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



Joined: 12 Nov 2007
Posts: 37

View user's profile Send private message

PIC16F616 PWM and ADC
PostPosted: Mon Jan 21, 2013 6:24 am     Reply with quote

Hi.

I need PWM output and ADC input my project.
PWM its OK, but ADC no read voltage and replace PWM duty cycle.
Its my config OK?

Code:

#include <16F616.h>
#device adc=8
//
#FUSES WDT                      //Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOPROTECT                //No code protected from reads
#FUSES IOSC4                    //INTOSC speed 4MHz
#FUSES NOMCLR                   //No master Clear pin enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES PUT                      //Power Up Timer
//
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(c)
//
#bit ECCPASE = 0x17.7 // PWM start/stop control bit (ECCPAS-register)
#bit ECCPAS0 = 0x17.4 // PWM Autoshotdown enable/disable control bit (ECCPAS-register)
//
#define S_ETEEN output_low(PIN_C4)
#define S_TAAKSE output_high(PIN_C4)
#define ECCPAS0_LED_OFF output_high(PIN_A0)
#define ECCPAS0_LED_ON output_low(PIN_A0)
//
//******************************************************************************
#define NOPEUS_MAX 400L
#define NOPEUS_START 60L
#define REFERENSSI 9
int8 kahva;
int8 kahva_old;
int8 ad_tulos;
int8 NOPEUS_OK;
int16 nopeus;
int16 kiihdytys_nopeus;
//
#include "Aliohjelmat.h"
//
void main()
{
   set_tris_a(0b111010); // 1=input, 0=output.
   set_tris_c(0b001111);
//
   output_float(PIN_C0);
   output_float(PIN_C1);
//
   setup_adc_ports(sAN6 | VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_16);
   read_adc(ADC_START_AND_READ);
   set_adc_channel(6);     
   delay_ms(20);
//
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_wdt(WDT_2304MS);
   restart_wdt(); 
   setup_timer_1(T1_DISABLED);
//
   setup_timer_2(T2_DIV_BY_1,99,1);
   ECCPASE = 1; // PWM-stop
   set_pwm1_duty(0L);
   setup_ccp1(CCP_PWM | CCP_SHUTDOWN_ON_COMP1);   
//
   setup_vref(VREF_HIGH|REFERENSSI);
   setup_comparator(CP1_C3_VREF | CP1_OUT_ON_A2 | CP1_INVERT);
//
   kahva_old = 0;
   ECCPAS0_LED_OFF;
//
   ad_tulos = read_adc();
   delay_ms(20);
   ad_tulos = ad_tulos + read_adc();
   delay_ms(20);
   ad_tulos = ad_tulos/2;
   delay_ms(20);
   NOPEUS_OK = 80L;
   if (ad_tulos > 38) NOPEUS_OK = 120L;
   if (ad_tulos > 64) NOPEUS_OK = 160L;
   if (ad_tulos > 89) NOPEUS_OK = 200L;
   if (ad_tulos > 115) NOPEUS_OK = 240L;
   if (ad_tulos > 140) NOPEUS_OK = 280L;
   if (ad_tulos > 166) NOPEUS_OK = 320L;
   if (ad_tulos > 192) NOPEUS_OK = 360L;
   if (ad_tulos > 217) NOPEUS_OK = 400L;
//
   while(1)
........
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 10:54 am     Reply with quote

How do you KNOW what's working and what's not?

Is this real hardware or simulation?

Make the whole thing simpler.

Reduce your code to the minimum.

Be consistent with your variable names, (you've got a mix of upper and lower cases).

Mike
jjude



Joined: 12 Nov 2007
Posts: 37

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 12:13 pm     Reply with quote

Mike Walne wrote:
How do you KNOW what's working and what's not?
Is this real hardware or simulation?
Mike

I see that the oscilloscope pwm works.
But by changing the voltage of the ADC does not change dutycycle.
Correct device is.
An earlier version of the program was changed from dutycycle program
code and this will not work
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 1:20 pm     Reply with quote

Quote:

#device adc=8

int8 ad_tulos;
int8 NOPEUS_OK;

ad_tulos = read_adc();
delay_ms(20);
ad_tulos = ad_tulos + read_adc();
delay_ms(20);
ad_tulos = ad_tulos/2;
delay_ms(20);
NOPEUS_OK = 80L;
if (ad_tulos > 38) NOPEUS_OK = 120L;
if (ad_tulos > 64) NOPEUS_OK = 160L;
if (ad_tulos > 89) NOPEUS_OK = 200L;
if (ad_tulos > 115) NOPEUS_OK = 240L;
if (ad_tulos > 140) NOPEUS_OK = 280L;
if (ad_tulos > 166) NOPEUS_OK = 320L;
if (ad_tulos > 192) NOPEUS_OK = 360L;
if (ad_tulos > 217) NOPEUS_OK = 400L;

You have been on this forum since 2007 and you still don't know the
size of CCS data types, and the largest numbers they can hold.
It's essential to know these things.

Quote:
#FUSES WDT

You shouldn't use Watchdog timer in a program that is still being
developed.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 2:43 pm     Reply with quote

This link shows a program which uses ADC to control PWM.
(Courtesy of PCM programmer)

http://www.ccsinfo.com/forum/viewtopic.php?t=40007&start=1&postdays=0&postorder=asc&highlight=

You'll need to change the chip.

What more do you need?

Mike
jjude



Joined: 12 Nov 2007
Posts: 37

View user's profile Send private message

PostPosted: Tue Jan 22, 2013 1:29 am     Reply with quote

I'm sorry!
The code works just fine, the problem was the rest of the code.
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