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

Help me take a look on my code

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



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

Help me take a look on my code
PostPosted: Mon Jun 20, 2011 10:33 pm     Reply with quote

Code:

#include <18F4520.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#include <flex_lcd.c>

void main(void)
{
//long  ADC_Result;
setup_adc(ADC_CLOCK_INTERNAL);//enables the a/d module
//and sets the clock to internal adc clock
setup_adc_ports(AN0); //sets all the adc pins to analog
set_adc_channel(0); //the next read_adc call will read channel 0

setup_ccp1(CCP_PWM);   
setup_timer_2(T2_DIV_BY_16,255,10);   //why the postscale will error when it is 0,must put in range of 1~16
set_pwm1_duty(512);      //Start with duty cycle of 50%.

//while (true) {
//   ADC_Result = read_ADC();
//   set_pwm1_duty(ADC_Result);         //Vary motor speed with ADC result.
//   set_pwm2_duty(ADC_Result);         //Vary motor speed with ADC result.
//   delay_ms(200);
//   }
}


I want to read the value on digital oscilloscope, I connect with PROBE, red wire is RC2 (CCP1), black is GND, why no result on the digital oscilloscope?
I have the result when I enable the
Code:

//while (true) {
//   ADC_Result = read_ADC();
//   set_pwm1_duty(ADC_Result);         //Vary motor speed with ADC result.
//   set_pwm2_duty(ADC_Result);         //Vary motor speed with ADC result.
//   delay_ms(200);
//   }

Code:

setup_timer_2(T2_DIV_BY_16,255,10);

This timer, T2_DIV_BY_xx......xx can put from 1 to 16 or more??
The PR2, 255, why even I put 1250 also can work in the pic??
Code:

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
setup_timer_2(T2_DIV_BY_16,255,10);

What is the different if compare this two code??
Just the same function?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 21, 2011 7:12 am     Reply with quote

The problem with your non-working program version is that at the very end of the program the program stops. You setup the whole processor and then, nothing ...
The compiler always inserts a sleep instruction at the end of main(), that's why you see nothing on the scope.

Solution, always insert an endless loop at the end of all your programs:
Code:
void main()
{
   ... do your stuff here

   while(1);   // loop forever
}
The compiler will show a warning on the while-loop. You can safely ignore this, or replace by:
Code:
for(;;);    // loop forever
This will do exactly the same as the while-loop without the compiler complaining.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

Re: Help me take a look on my code
PostPosted: Tue Jun 21, 2011 7:23 am     Reply with quote

pacman91 wrote:
Code:
setup_timer_2(T2_DIV_BY_16,255,10);

This timer, T2_DIV_BY_xx......xx can put from 1 to 16 or more??
This will depend on the hardware of your specific PIC processor. Read the datasheet.
Whenever programming for the PIC you always have to study the datasheet for your processor. Keep the datasheet close because you will need it many times.

Quote:
The PR2, 255, why even I put 1250 also can work in the pic??
255 is the maximum value for an int8.
1250 is an int16.
PR2 only accepts int8, so the compiler will only use the lower 8 bits of the 1250 value. --> this becomes 226 and that is what you see when you say 'it works'.

Quote:
Code:
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
setup_timer_2(T2_DIV_BY_16,255,10);

What is the different if compare this two code??
Just the same function?
No, it is not the same function. The names and parameters are different. Again, study the datasheet for your PIC hardware to understand the differences.
You can find a list of allowed parameters for your processor in the header file 18F4520.h
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 22, 2011 7:48 am     Reply with quote

ok thx bro

while(1)
{
}


i tried it
it works, thx
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