|
|
View previous topic :: View next topic |
Author |
Message |
Dorus
Joined: 19 Jan 2012 Posts: 1
|
I'm new, can someone help me? |
Posted: Thu Jan 19, 2012 4:04 am |
|
|
Hello to you all,
I recently bought some stuff for the PIC and with that I bought CCS Compiler. I'm very new to the PIC and CCS, I am used to the Arduino code language.
I'm trying to get a analog input from pin A0 on a PIC16F690 with the internal oscillator on 8MHz.
Can someone tell me how to do such an analog reading?
Thank you,
Dorus |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Jan 19, 2012 5:53 am |
|
|
Start by looking at the examples. Things like EX_ADMM10.c, show taking a number of readings, and displaying the min/max over the RS232.
Unfortunately, there are several things wrong with this (and some of the other samples here), with using a clock for the ADC that is not recommended by the chip manufacturer (Duh....). Also, they don't show how to use the internal clock.
So, though not normally done, I will post a specific minimum version for your chip:
Code: |
#include <16F690.h> //Specifies chip
#device adc=10 //resolution of the ADC to use
#FUSES NOWDT, INTRC_IO, PUT, NOMCLR, NOBROWNOUT, NOIESO, NOFCMEN
//Fuses - essential setup the options INTRC etc..
#use delay(clock=8000000)
//Specify what clock you are running - when using the internal clock, this also
//configures it automatically.
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
//and configure the RS232 port for I/O
//This stuff should all be at the start of your code _before_ you include
//other things. Either in the 'main' like this, or in a separate 'include' file
void dummy(void) {
int8 nothing;
nothing=rs232_errors;
}
//This prevents an annoying compiler warning otherwise generated if you don't
//'use' the rs232_errors value. Uses no space, since the unused code is
//optimised away!....
void main(void) {
int16 ival; //configure variables at the start of each function
//in CCS an 'int' is 8bit, so need to use an int16 for an ADC reading.
setup_adc_ports(sAN0); //Select what ADC channels are enabled
setup_adc(ADC_CLOCK_DIV_4); //Select ADC clock - must give 2uSec/cycle min
//So with 8MHz CPU clock/4 (master peripheral clock 2MHz), needs a further
//division by 4 to meet this.
setup_comparator(NC_NC_NC_NC); //Just turn off the comparator
setup_spi(FALSE); //and the SPI
set_adc_channel(0); //select ADC channel to read
delay_us(10); //must delay before taking a reading
//Now code needs to loop to work.
do {
ival=read_adc(); //Read the value from the ADC - will be 0 to 1023
printf("ADC value %ld\n\r",ival); //send the value to the RS232
delay_ms(1000); //Pause for 1 second between readings
} while (TRUE);
}
|
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Jan 19, 2012 5:20 pm |
|
|
Welcome to the forum Dorus -
you may not know it yet - but
a grand master of CCS C just did
your homework, giving you a more concise
example of "how its done" than you will ever get
in class. In reality anyone who really WANTS
to get good at programming PICS - can't do better
than to:
A- study the CCS manual and examples
B- expend some effort pursuing your pic challenges
C- pay close attention to the excellent knowledge base
that can be found on this board.
PLUS the search function will show you so much good code
as well as code to avoid -
|
|
|
|
|
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
|