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

please help me here...

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



Joined: 05 Sep 2005
Posts: 8

View user's profile Send private message

please help me here...
PostPosted: Tue Sep 06, 2005 8:53 am     Reply with quote

#include <p18f452.h>
#include <adc.h>
#include <delays.h>
#include <stdlib.h>
#include <usart.h>

#pragma romdata CONFIG
_CONFIG_DECL(_CONFIG1H_DEFAULT & _OSC_HS_1H,
_CONFIG2L_DEFAULT & _BOR_ON_2L & _BORV_42_2L,
_CONFIG2H_DEFAULT & _WDT_OFF_2H,
_CONFIG3H_DEFAULT,
_CONFIG4L_DEFAULT & _LVP_OFF_4L,
_CONFIG5L_DEFAULT,
_CONFIG5H_DEFAULT,
_CONFIG6L_DEFAULT,
_CONFIG6H_DEFAULT,
_CONFIG7L_DEFAULT,
_CONFIG7H_DEFAULT);
#pragma romdata

int result ;

void main(void)
{

OpenADC(ADC_FOSC_32 &
ADC_LEFT_JUST &
ADC_1ANA_2REF,
ADC_CH0 &
ADC_INT_OFF) ;

OpenUSART(USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 129) ;

while(1)
{
Delay10TCYx(5) ;
ConvertADC() ;
while( BusyADC() )
;

result = ReadADC() ;

WriteUSART(result) ;
putsUSART(" ~ ") ;
while( BusyUSART() )
;
}

}


i want to send in a 1kHz sine wave, A/D it and send the result to pc...
i'm running PIC at 20MHz... what baud rate should i set to?? will there be any problem to convert and send data to pc directly??
thank you for helping~~
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 06, 2005 9:13 am     Reply with quote

Note to others: This code is for the C18 compiler.

Note to poster: What sampling rate do you require?
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

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

PostPosted: Tue Sep 06, 2005 9:13 am     Reply with quote

Are you sure you're using the CCS compiler? With all these #pragmas, looks like you're using the Hi-Tech compiler.

Edited:

Oh, I see Mark has pressed "submit" a few seconds before me. I don't know if it's just because it's the day after a 3-day weekend, but there is a LOT of confusion in today's posts so far this morning Rolling Eyes
hhyong99



Joined: 05 Sep 2005
Posts: 8

View user's profile Send private message

PostPosted: Tue Sep 06, 2005 9:24 am     Reply with quote

sorry, i'm using C18 compiler...
how to define sampling rate in programming code?? if i want to sample 1kHz sine wave, at least i need 2kHz sampling rate, right?? but in the code, i'm using 9600 baud rate, should i change this??
thank you for still helping me...
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 06, 2005 9:28 am     Reply with quote

A 2KHz sample on a 1KHz signal would not be very good. Think about it. Can you represent a sine wave with 2K/1K = 2 points?
hhyong99



Joined: 05 Sep 2005
Posts: 8

View user's profile Send private message

PostPosted: Tue Sep 06, 2005 9:38 am     Reply with quote

haha... Mark, you are right indeed... then what's the best sampling rate for this??
Ttelmah
Guest







PostPosted: Tue Sep 06, 2005 9:40 am     Reply with quote

The minimum sample rate, using conventional sampling for intelligable speech, is in the order of 4500 samples per second, and this is _borderline_.
I'd suggest you look at having an interrupt every 1/10000th second, and on alternate interrupts, start the ADC, and on the next one, read it. Thi would allow 5000 samples/sec.
As for how to do this, you need to go and ask on a C18 forum. This forum is for CCS C, and though some parts are 'generic', details of how to handle tight timings, and interrupts, are specific to the compiler.

Best Wishes
hhyong99



Joined: 05 Sep 2005
Posts: 8

View user's profile Send private message

PostPosted: Tue Sep 06, 2005 9:47 am     Reply with quote

Ttelmah, must i have adc interrupt for this?? cos i don't really know how that's work...
thank you...
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 06, 2005 11:05 am     Reply with quote

hhyong99 wrote:
Ttelmah, must i have adc interrupt for this?? cos i don't really know how that's work...
thank you...


He didn't say to use an ADC interrupt but rather to use a timer interrupt.

Or are you trying to say that you do not know how to use the ADC. If so then:

ADC means Analog to Digital Convertor

How do you suppose that you are going to convert that analog signal to a digital one without using the ADC.
hhyong99



Joined: 05 Sep 2005
Posts: 8

View user's profile Send private message

PostPosted: Tue Sep 06, 2005 11:43 am     Reply with quote

i know what is ADC... but i don't know whether to use ADC or timer interrupt for that...

if i want to set my sampling rate at 10kHz, osilloscope the result of the convertion at the one of my output pin, can that be done?? how can i send the data to the output pin??
Ttelmah
Guest







PostPosted: Tue Sep 06, 2005 4:58 pm     Reply with quote

The basic answer to what you have now asked, is probably 'no'.
Though this is still the wrong forum, I will answer, since other people using the CCS compiler may well want to do a similar thing.
The first thing to understand, is that your sampling _must_ be regular. Any change in the sample rate, will result in a change in the frequencies represented by the data. Hence I suggested sampling at 5KHz, but using a timer ticking at 10KHz. What you do, is at the first 'tick', start the ADC reading, then at the second, take the result. This you then send to the outside world. Then repeat. Using a serial port at 57600bps, you could just send the data in the time before the next sample, and the rate will be regular. The output data however this will be as a serial stream, and will not be something that would make much/any sense watched on an oscilloscope.
The problem will be _time_, and the amount of data involved. Since there will be 8bits of data at every sample, there will be 40000 bits/second of data. Using 10KHz instead of 5KHz, gets very tight on finding enough time to take the reading and send it.
Proper analog sampling chips, use 'delta' modulation (only recording the change from the last sample), to keep the data rate down.
Unless you are going to write this in CCS C, please go and ask your questions on a C18 forum.

Best Wishes
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