|
|
View previous topic :: View next topic |
Author |
Message |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
Timer & loops |
Posted: Tue Apr 23, 2013 10:48 am |
|
|
Hello everyone,
I have a problem in my code, i am using the ccp module of the pic16f877a, to generate 300hz pulses for my digital servo, but when i use infinite loop to always monitor the value of the variable, (Proteus just go crazy) overflow
here is the code
Code: |
#include <16f877a.h>
#use delay(clock = 4Mhz)
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS PIN PIN_D1
#define LCD_RW PIN PIN_D2
#define LCD_DATA_PORT D
#define LCD_TYPE 2
#include <lcd4.c>
int32 val;
int i = 0;
void main()
{
lcd_init();
delay_ms(50);
setup_adc(adc_clock_internal);
setup_adc_ports(PIN_A0);
set_adc_channel(AN0);
delay_ms(10);
output_low(PIN_C2);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 210, 1);
do
{
val = read_adc();
delay_ms(10);
lcd_gotoxy(1,0);
printf(lcd_putc, "%lu", val);
}while(true);
}
|
Thanks in advance,
z3ngew |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 23, 2013 12:02 pm |
|
|
Tell us what happens with REAL hardware.
Nobody here can answer Proteus questions!
Mike |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Tue Apr 23, 2013 2:01 pm |
|
|
I'm sry for this rush question, the problem was in the code, but i took care of it, and here is the code [analog pot change the duty cycle of 300hz train generated using ccp module]
Code: |
#include <16f877a.h>
#DEVICE adc = 10;
#use delay(clock = 4Mhz)
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS PIN PIN_D1
#define LCD_RW PIN PIN_D2
#define LCD_DATA_PORT D
#define LCD_TYPE 2
#include <lcd4.c>
int32 val;
int i = 0;
void main()
{
lcd_init();
delay_ms(50);
setup_adc(adc_clock_internal);
setup_adc_ports(ALL_ANALOG);
delay_ms(10);
output_low(PIN_C2);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 210, 1);
do
{
set_adc_channel(0);
val = read_adc();
set_pwm1_duty(val/2);
delay_ms(10);
lcd_gotoxy(1,0);
printf(lcd_putc, "%lu", val);
}
while(true);
}
|
Good Luck,
z3ngew |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 23, 2013 2:20 pm |
|
|
Does this mean problem is solved?
If so, you can amend title.
Mike
Last edited by Mike Walne on Tue Apr 23, 2013 2:23 pm; edited 1 time in total |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Tue Apr 23, 2013 2:21 pm |
|
|
Yes, it work just fine in simulation, and in real life |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Wed Apr 24, 2013 1:04 am |
|
|
There are several things I will comment that are faulty with the code as published though....
Code: |
setup_adc(adc_clock_internal);
//Read the data sheet. Is this clock recommended at your CPU clock
//rate?. Accuracy will be spoiled by this.....
setup_adc_ports(ALL_ANALOG);
//Unless you are using all the pins as analog, don't select this
//Only select the pins you are going to use. Every pin that is selected to
//the analog multiplexer, increases the potential noise sources.
//Microchip have notes about this. Again degrades accuracy...
delay_ms(10);
output_low(PIN_C2);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 210, 1);
do
{
set_adc_channel(0);
//Read the data sheet again. Is Tacq being satisfied here?.
//again accuracy will be killed....
//It'll work, since you are re-selecting the same channel
//Why?. select the channel once unless you need to change it
//but if you were changing channels, it wouldn't work...
val = read_adc();
|
|
|
|
|
|
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
|