View previous topic :: View next topic |
Author |
Message |
jjude
Joined: 12 Nov 2007 Posts: 37
|
PIC16F616 PWM and ADC |
Posted: Mon Jan 21, 2013 6:24 am |
|
|
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)
........
|
|
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Jan 21, 2013 10:54 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
jjude
Joined: 12 Nov 2007 Posts: 37
|
|
Posted: Mon Jan 21, 2013 12:13 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 21, 2013 1:20 pm |
|
|
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.
You shouldn't use Watchdog timer in a program that is still being
developed. |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
![](templates/subSilver/images/spacer.gif) |
jjude
Joined: 12 Nov 2007 Posts: 37
|
|
Posted: Tue Jan 22, 2013 1:29 am |
|
|
I'm sorry!
The code works just fine, the problem was the rest of the code. |
|
![](templates/subSilver/images/spacer.gif) |
|