Jump to content
Sign in to follow this  
Johan Matthee

PIC12F675 Using ADC and TMR0 at the same time.

Recommended Posts

Hi

 

I can write code to get TMR0 Interrrupt to control my Real Time Clock.

Use Hardware or Software Interrupts to call Functions to execute the all the code needed to control my program.

 

What I can't do is call a Function to do an A/D  conversion without setting bit ADIE in Register PIE1.

When bit ADIE is set, the ADC Function works flawlessly exept that I lose my TMR0 capabillity.

My finding was that both TMR0 interrupt and A/D Conversion work exellent but I can't get them to work together.

 

I do need help here!

 

#include <p12f675.h>


// Global variables declaration

char INTCNT = 244;//Aproximate count for one sec
char SECADJ = 7;	//correction for one sec
char ADH;  //Storage for A/D converter High bite
char ADL;  //Storage for A/D converter LOW bite
char Index = 3;  //future use
char Pulse = 0;  //future use

//Configuration bits
asm
{
list p=12F675
__config H'318C' 
} 



void on_init(void)
{
// initialize peripherals and the application

   option_reg = 3;    //set prescaler to 1:16 (4.096mS)
   enable_interrupt( GIE ); //enable global interrupts
   set_bit( intcon, T0IE );//set TMR0 interrupt
   //set_bit(pie1, ADIE);	//enable A/D converter interrupt
   set_bit( intcon, PEIE ); //all unmasked interrupts
   trisio = 0x0B;    //GP:0,1&3 inputs. GP:2,4&5 outputs
   ansel = 0x13;  //AN0-PIN7 & AN1-PIN6 Analog inputs and 8Tosc selected
   cmcon = 7; 	 // No comparator selected   
   gpio = 0; 	 //all outputs to zero
   ADH = adresh; //address high
   ADL = adresl; //address low
}

//List of all functions
void on_init(void);  //sets up interrupts, ports, pins etc.
void on_adc(void);	//A/D conversion



void interrupt( void ) { //generate 1 sec pulse
INTCNT--;
            if( INTCNT == 0 ){
INTCNT = 244;	//reset count
SECADJ--; //dec SECADJ every second
gpio ^= 0x04; //compliment GPIO:2 (LED ON/OFF)
}
	 
 if( SECADJ == 0 ) { //time to add correction
    INTCNT ++; //add correction to 1 sec
    SECADJ = 7; //reset FUDGE
	 }	
 clear_bit( intcon, T0IF ); //clear TMR0 bit T0IF
 on_adc(); //take new reading at ADC
}
    
void on_adc(void) {
 
 clear_bit( pir1, ADIF );
 adcon0 = 0x01;//Left justified, Vdd, GO_DONE = 0, ADON   
 delay_us( 50 );	//small delay
 set_bit( adcon0, GO );//start A/D Converter
 
 if (( ADH >= 0 )&&( ADH <= 5 )) {	//220V - 215V
set_bit( gpio, GPIO5 );
 }
 if (( ADH >= 6 )&&( ADH <= 10 )) {	//214V - 210V
  	 set_bit( gpio, GPIO4 );
 }
 if (( ADH >= 11 )&&( ADH <= 15 )) {	//209V - 205V
	 set_bit( gpio, GPIO5 );
 }
 if (( ADH >= 16 )&&( ADH <= 20 )) {  //204V - 200V
	 set_bit( gpio, GPIO4 );
 }	
 if (( ADH >= 21 )&&( ADH <= 25 )) {  //199V - 195V
	 set_bit( gpio, GPIO5 );
 }
 if (( ADH >= 26 )&&( ADH <= 30 )) {	//194V - 190V
	 set_bit( gpio, GPIO4 );
 } 
 if (( ADH >= 31 )&&( ADH <= 35 )) {	//189V - 185V
	 set_bit( gpio, GPIO5 );
 }
 if (( ADH >= 36 )&&( ADH <= 40 )) {	//184V - 180V
	 set_bit( gpio, GPIO4 );
 }
 if (( ADH >= 41 )&&( ADH <= 255 )) {	//Less then 180V
   set_bit( gpio, GPIO5 );
 }
}

  
main()
{	
 on_init(); // do initialization once
	 
 
	 while(1)  //endless loop
	 {
   clear_wdt(); //reset Watchdog timer	
	 }
}

Share this post


Link to post
Share on other sites
Guest Dave

Johan,

 

To used ADC without interrupts:

1) Set the GO/DONE bit (to start the A to D conversion)

2) Wait until GO/Done is clear (cleared by the hardware when conversion completed).

3) Read ADRESH (and ADRESL if required) to get result.

 

Regards

Dave

Share this post


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...
Sign in to follow this  

×
×
  • Create New...