Jump to content
Sign in to follow this  
j3n4

Emulator And True Hardware

Recommended Posts

Hi,

im wrote a little test program to read ADC and out the result on LCD.

He works fine in emulator but when i try on my Hardware it does not work.

 

He print the "BootUp..." Message and stop.

I think ADC read is wrong but im not have any ideas.

 

Can you help me?

 

#include <system.h>
// Add the following after #include <system.h> in you source file:
//
#define LCD_ARGS 	2,	1, PORTB, TRISB, PORTC, TRISC, 3, 2, 1 
//
#include <lcd_driver.h> // include the LCD template code

//Target PIC16F84 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _XT_OSC & _CP_OFF

//Set clock frequency
#pragma CLOCK_FREQ	4000000

void interrupt( void )
{

//Handle timer0 interrupt
if( intcon & (1<<T0IF) )
{
	clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
}

}

void main( void )
{

volatile bit A2D_not_done@0x0FC2.2;    // bit 2 of adcon0

//Configure port A
trisa = 11111111b;    // ALL PORT A inputs
//Configure port B
trisb = 00000000b;   //set portb as output.

//Configure port B
trisc = 00000000b;   //set portb as output.

//Initialize port B
portb = 0;

//Initialize port C
portc = 0;

// initialize ADC 
   adcon1 = 00000010b;  
   
//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

//Set timer 2 prescaler rate
clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1
clear_bit( t2con, T2CKPS0 );
//Set timer 2 postscaler rate
clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1
clear_bit( t2con, TOUTPS2 );
clear_bit( t2con, TOUTPS1 );
clear_bit( t2con, TOUTPS0 );
//Set timer 2 mode (enable or disable)
clear_bit( t2con, TMR2ON ); //enable timer 2


//Enable interrupts (Timer0)
intcon = 0xA0;

short resval;

lcd_setup();
lprintf( "BootUp...\n" );
delay_ms( 2000000 );

//Endless loop
while( 1 ){

adcon0 = 10000001b;            // Read from AN0
   delay_us(20);
   set_bit(adcon0, 2);            // INNER GATES
   while (A2D_not_done);        // Wait for a2d done flag to change
   resval = adresh; 
   lcd_clear();
   if(resval >1)lprintf(">0");
   if(resval <1)lprintf("<0");
   delay_ms( 1000000 );
}
}

 

Best Reguards

J3n4

Share this post


Link to post
Share on other sites

Is there some reason your only reading half the ADC result? And the upper half at that?

Share this post


Link to post
Share on other sites
Is there some reason your only reading half the ADC result? And the upper half at that?

True,

but the result its the same with adjustment.

My procedure is wrong.

I resolved with this code:

 

// Read A0 for desired setpoint
adcon0 = 10000001b; 
// Wait the required acquisition time (datasheet)
delay_10us(2);
//set_bit(adcon0, 2);            // OUTER GATES
// Start the conversion
adc_go = 1;
// Wait until it is done
while (adc_go);
retval = (short)adresh;
retval = retval << 8;
retval |= adresl;

Now work fine.

 

Best Reguards

j3n4

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