Jump to content

Recommended Posts


I am trying to get the a2d to work with a 12f675.

Has anyone any experience with this pic?


I have simulated the a2d using a 16f877 with sourceboost IDE, but unfortunately cannot simulate with the 12f675.


Can anyone see where I'm going wrong.


#include <system.h>


//Target PIC12F675 configuration word



//Set clock frequency

#pragma CLOCK_FREQ 4000000



volatile bit gp0@GPIO.0;//analog i/p

volatile bit gp1@GPIO.1;//analog i/p

volatile bit gp2@GPIO.2;//motor drive


volatile bit gp4@GPIO.4;//enable

volatile bit gp5@GPIO.5;//motor drive


#define motor_clockwise gp2 //pointer travels up

#define motor_anticlockwise gp5 //pointer travels down

#define enable gp4


unsigned char AD_Result;


void a2d (char);


void interrupt( void )


//Handle timer0 interrupt

if( intcon & (1<<T0IF) )


clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit



//Handle timer1 interrupt

if( pir1 & (1<<TMR1IF) )


clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit




void main( void )


//Configure GPIO port

trisio = 0x03;//gpio0 & gpio1 as a2d input.

gpio = 0x00;


cmcon = 0;//comparator off.

//ansel = 0;


//Set Timer0 mode

clear_bit( option_reg, T0CS ); //configure timer0 as a timer

//Set prescaler assignment

clear_bit( option_reg, PSA ); //prescaler is assigned to timer0

//Set prescaler rate

clear_bit( option_reg, PS2 ); //prescaler rate 1:2

clear_bit( option_reg, PS1 );

clear_bit( option_reg, PS0 );

//Set timer0 source edge selection

set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin


//Set timer 1 prescaler rate

clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1

clear_bit( t1con, T1CKPS0 );

//Set timer 1 mode

clear_bit( t1con, TMR1ON ); //disable timer 1


//Enable interrupts (Timer0)

intcon = 10100000b;

a2d(AD_Result);//pot i/p.

//Endless loop

while( 1 )



enable = 1;

if(AD_Result>5)//arbitory value to prove a2d returning sensible value.


motor_clockwise = 0;

motor_anticlockwise = 1;

motor_clockwise = 1;

motor_anticlockwise = 0;






void a2d (char AD_Result)










set_bit(adcon0,ADFM); // Turn on the A/D converter, select channel 0.








volatile bit Go_Done@ADCON0.1;

volatile unsigned char ad_h@ADRESH;

volatile unsigned char ad_l@ADRESL;


unsigned int shiftH;

unsigned int shiftL;


set_bit(adcon0,Go_Done); // Start conversion




shiftH = ad_h;

shiftL = ad_l;


AD_Result = (shiftH<<2) | (shiftL>>6); // Combine the two registers to get full 10 bits.


adcon0 = 0; // Shut off the AD converter.


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...