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

transmitting a step sine wave using PIC

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







transmitting a step sine wave using PIC
PostPosted: Tue Mar 03, 2009 8:24 pm     Reply with quote

I need to transmit a step sinewave so that it can be viewed in the oscilloscope using PIC18F46K20. However I do not know how to transmit a signal to the oscilloscope. I'm using MPLAB.
Can anybody suggest the code to do this.

Thanks
Maheshi
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 03, 2009 8:28 pm     Reply with quote

See this CCS example file:
Quote:
c:\Program Files\picc\Examples\Ex_Sine.c
Maheshi
Guest







transmitting a step sine wave to the oscilloscope
PostPosted: Wed Mar 04, 2009 7:37 pm     Reply with quote

I'm sorry but how do I access the file. I am new to this site.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Mar 04, 2009 8:42 pm     Reply with quote

If you have the CCS compiler installed on your PC you should have a directory on your hard disk called picc\Examples. That directory should have a file called Ex_Sine.c.

I question if you are expecting to get an analog signal from your PIC? The PIC is inherently a digital device. The closest it comes to an analog output is PWM, but that is still a square wave until it is put through a low pas filter. You could drive a D/A chip from the PIC and use your scope to look at the analog output of the D/A chip. I don't know of any PICs with built in D/A.
_________________
The search for better is endless. Instead simply find very good and get the job done.
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Mar 05, 2009 8:47 am     Reply with quote

Maheshi,

Here is how I did it using PWM. This isn't the complete program - you really don't want to go through a few thousand lines of code -- but hopefully this will help you get on the right track.

Code:

const long int s_table[] = {128,176,218,246,255,246,218,176,
                            128,79,37,9,0,9,37,79};

// This function steps the PWM duty cycle value when Timer0 overflows.
#int_rtcc
void pwm_tone(void) {
    set_pwm1_duty(s_table[s_table_idx++ % 16]);
    set_timer0(t0preload);
}

void main() {

    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8|RTCC_8_BIT);
    setup_timer_2(T2_DIV_BY_1,63,1);
    setup_ccp1(CCP_PWM);
    set_pwm1_duty(0);
 ...



How does it work? TIMER0 interrupts on a regular basis depending on your clock frequency. Every time it does, the PWM duty cycle is set from the s_table values. I derived those using Excel -- what is shown here gave me a very nice looking sine wave with just a little ripple. I have since switched to a table with only 8 values, which saves CPU time at the expense of a little less smooth waveform.

The t0preload value is used to determine the audio frequency of the resulting sine wave. Again, the values to use depend entirely on your PIC's clock speed. For example, I'm using a PIC18F4620 running at 32 MHz. Preloading Timer0 with a value of 18 will give a 500 Hz wave.

The output of the PWM is low-pass filtered using a series 5K Ohm resistor and a .1 uF capacitor to ground. The filter component values may need to be adjusted depending on your choice of frequencies. Generally, the higher your PWM frequency is the better off you'll be.

Of course after typing all of that I remembered this, complete with a picture of what I ended up with:

http://www.ccsinfo.com/forum/viewtopic.php?t=36540
Maheshi
Guest







sine wave
PostPosted: Tue Mar 10, 2009 10:30 am     Reply with quote

Thanks a lot Dbotkin . Is it possible for you to post ur entire main program. Im still trying to output the sine wave so I can start working with it to modulate and demodulate signals.
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