Jump to content

Recommended Posts

Hi Guys,

 

This has to be an error by me, but I can't presently see it.

 

I'm trying to do a simple polled ADC on port RA1, but whatever I try, I get zero in adresh:adresl from both the simulator and real hardware.

 

Here's a heavilly stripped down extract from the application:

 

/********************************************************************
Filename:	Try.c
*/


#include <system.h>

//Target PIC16F676 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_ON & _CP_OFF & _INTRC_OSC_NOCLKOUT
//		_INTRC_OSC_NOCLK not valid??
//#pragma DATA _CONFIG, 0x3FF4

//Set clock frequency
#pragma CLOCK_FREQ	4000000

#define	ADCH0	(0x00 << CHS0)
#define	ADCH1	(0x01 << CHS0)
#define	ADCH2	(0x02 << CHS0)
#define	ADCH3	(0x03 << CHS0)
#define	ADCH4	(0x04 << CHS0)
#define	ADCH5	(0x05 << CHS0)
#define	ADCH6	(0x06 << CHS0)
#define	ADCH7	(0x07 << CHS0)

char tick;
char hundredths;
char tenths;
char seconds;
char adc;


/****************************************************/

void interrupt( void )
{
// timer 0 interrupt
if ( intcon&0x20 && intcon&0x04 )
{
	tmr0 = 6;
	clear_bit( intcon, T0IF );

	tick = 1;
}
}

/*******************************************************/

main()
{
// initialize peripherals and the application
option_reg = 0x5F;

// IO ports setup
wpu = 0;
trisa = 0x2B;		// Port RA5, MCLR, RA1 and RA0 are inputs
porta = 0x00;		// All low (pump off, SEG_A On).
trisc = 0x00;		// Port C is all outputs.
portc = 0x00;		// All SEGs On.

cmcon = 0x07;		// TODO Port-A says 0x05; fig 6-2 says 0x07?
ansel = ANS1;		// RA1 is LDR input
//adcon1 = 0x50;		// Fosc/16
adcon1 = 0x70;		// internal R/C
adcon0 = ADCH1|(1<<ADON)|(1<<GO);	// AN1 and go.

// initialise some data.
tick = 0;
adc = 0;
hundredths = 0;
tenths = 0;

// timer 0 setup
tmr0 = 6;

// enable peripheral and global interrupts
intcon = 0xE0;



while(1)
{
	if( tick )
	{
		tick = 0;

		// every tenth of second
		if ( ++hundredths >= 10 )
		{
			hundredths = 0;

			// every second
			if ( ++tenths >= 10 )
			{
			    tenths = 0;

			    // Read the A/D
			    adc = adresh;

			    // Start the next reading.
			    adcon0 = ADCH1|(1<<ADON)|(1<<GO);
			    // this is => adcon0 = 0x07
			}
		}
	}
	// tum-ti-tum .. so much waiting around, maybe another coffee?
}
}

 

Anyone see and correct my deliberate mistake, please?

 

Thanks,

Gordon

Link to post
Share on other sites

I appear to have a working A/D, though not without some notable problems. FYI, here's how it went.....

 

I found one bug -- using a bit index as a value in ansel, however it still wouldn't work as there were some other problems.

 

To cover all the possible timing/delay situations (too quick, too slow, various register settings) I set each part of adcon0 separately, with a fairly substantial delay between them. I doubt I need them all, or so long, but I don't have time right now to tune them or remove surplus delays.

 

Despite using the slower of the recommended Tad setting (Fosc/16 on the 4MHz intrc clock should be 4us), Microchip's MPLAB-Sim complained that Tad was too low at 1.6us. Changing it to use the Frc oscillator actually gave me back a DONE signal from that simulator, but still did not from the SourceBoost simulator (?). I then tried it in some hardware and got something in adresh:adresl.

 

I then took the modified code and grafted it back into the real application, which is a touch more difficult to debug and, at last, I get back some results.

 

A litle tidying should see it done, if rather overdue.

 

Here's the code I ended up with.

 

I'd earlier spent some more time cutting out more code superlfuous to the problem.

 

There's really not much left.

 

#include <system.h>

//Target PIC16F676 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _CP_OFF & _INTRC_OSC_NOCLKOUT

//Set clock frequency
#pragma CLOCK_FREQ    4000000

#define    ADCH1    (0x01 << CHS0)

char adc;


/****** main ******/
main()
{
   // initialize peripherals and the application
   option_reg = 0x5F;

   // IO ports setup
   wpu = 0;
   trisa = 0x2B;        // Port RA5, MCLR, RA1 and RA0 are inputs
   porta = 0x00;        // All low (pump off, SEG_A On).
   trisc = 0x00;        // Port C is all outputs.
   portc = 0x00;        // All SEGs On.

   cmcon = 0x07;        // TODO Port-A says 0x05; fig 6-2 says 0x07? -- 0x07 appears saner.
//    ansel = ANS1;        // <<=== wrong!
   ansel = (1<<ANS1);        // RA1 is LDR input
//    adcon1 = 0x50;        // Fosc/16
   adcon1 = 0x70;        // Frc

   // initialise some data.
   adc = 0;

   // enable global, peripheral, T0 interrupts

   intcon = 0;

   while(1)
   {
       char i;

       // Setup the A/D to read
       adcon0 = ADCH1;        // select AN1

       for( i=30; i; i-- )    // Pause for no known reason .. desperation setting in here.
       {}

       set_bit( adcon0, ADON );    // Start the A/D

       for( i=30; i; i-- )    // Pause for signal acquisition.
       {}

       set_bit( adcon0, GO);    // start the conversion

       while( adcon0&(1<<GO) )    // wait for a `done' indication.
       {}

       adc = adresh; // get the result ( never gets here! )
       i = 0;    // somewhere to hang a breakpoint.
   }
}

Link to post
Share on other sites
  • 8 months later...

Just for the record, I've just made an update to this project and the ADC function still does not work in the simulator on SourceBoost (now the V6.55 release candidate).

 

FWIW, another project I'm working on right now uses the PIC12F683 .. and again the ADC doesn't work in the simulator.

 

In both cases it works in MPLAM. The 16F676 version works in real hardware; I haven't yet put the 12F683 version into real hardware.

Link to post
Share on other sites
Just for the record, I've just made an update to this project and the ADC function still does not work in the simulator on SourceBoost (now the V6.55 release candidate).

 

FWIW, another project I'm working on right now uses the PIC12F683 .. and again the ADC doesn't work in the simulator.

 

In both cases it works in MPLAM. The 16F676 version works in real hardware; I haven't yet put the 12F683 version into real hardware.

 

Oh Sugar .. I guess this is the problem:

# A/D converter - Only 16F877A and compatible devices supported.

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