Jump to content

Recommended Posts

Hi,

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.

 

CODE
#include <system.h>

 

//Target PIC12F675 configuration word

#pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _CPD_OFF & _INTRC_OSC_NOCLKOUT & _CP_OFF

 

//Set clock frequency

#pragma CLOCK_FREQ 4000000

 

//Outputs

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;

}

 

}//while

}//main

////////////////////////////////////////////////////////////////////////////////////////////

void a2d (char AD_Result)

{

clear_bit(ansel,ADCS2);

set_bit(ansel,ADCS1);

clear_bit(ansel,ADCS0);

clear_bit(ansel,ANS3);

clear_bit(ansel,ANS2);

clear_bit(ansel,ANS1);

set_bit(ansel,ANS0);

 

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

clear_bit(adcon0,VCFG);

clear_bit(adcon0,CHS1);

set_bit(adcon0,CHS0);

set_bit(adcon0,ADON);

 

set_bit(gpio,gp0);

 

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

 

delay_10us(2);

 

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.

Guest
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.

Loading...
×
×
  • Create New...