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 support@ccsinfo.com

A to D conversion with the 18f452

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







A to D conversion with the 18f452
PostPosted: Thu Apr 10, 2008 2:26 pm     Reply with quote

Hello,
I've been trying to figure out how to program an A to D in C code. I have one working in assembly because I'm a little more comfortable with that language. The problem is I have some difficult calculations to do that would be a nightmare in assembly so I'm converting everything over to C. Here is the working assembly code for the 18f452 and I was just curious if anyone could assist me in converting it over. I have mostly everything commented so its friendly.

Start
clrf TRISB; set PORTB as output
bsf TRISA,0; set PORTA as input
movlw 0x41; (Fosc/8) AD0=set and AD conversion GO
movwf ADCON0
movlw 0x8e; right justified, 7digital and 1 analog channel
movwf ADCON1
bac
nop
nop
nop
nop
bsf ADCON0,GO; set bit 2 of ADCON0 register
nop
nop
spl
btfsc ADCON0,GO; Start A/D
bra spl

; bsf ADCON0,GO
sp2
; btfsc ADCON0,GO
; bra sp2
movff ADRESL,PORTB; send A/D value to PORTB
bra bac
end

Thanks and I'm looking forward to your responses.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 10, 2008 2:37 pm     Reply with quote

Example:
Code:
#include <18F452.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=================================
void main()
{
int16 result;

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(15);

while(1)
  { 
   result = read_adc();
   printf("%LX ", result);
   delay_ms(500);
  }

}

This assumes your PIC has a 20 MHz crystal and that your analog input
signal is connected to the AN0 pin on the PIC. It also assumes that you
have your PIC board connected to the serial port on your PC, and that
you are running a terminal program such as HyperTerminal on your PC.
ongrongsan



Joined: 01 May 2013
Posts: 1
Location: Malaysia

View user's profile Send private message

Transfer the analog data to visual basic by through UART and
PostPosted: Wed May 01, 2013 6:40 am     Reply with quote

Thank for your coding, if I want transfer the analog data to visual basic by through UART and to control the I/O using visual basic. It's possible?
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed May 01, 2013 7:18 am     Reply with quote

Hi,

Yes, of course it's possible, why not? The PIC can send and receive data via
the UART, and Visual Basic can send and receive data using the PC COM port
and the MSCOMM control. Very simple to do this!

BTW, not that it's particularly relevant, but you are responding to a 5 year old thread!

John
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