Jump to content

Recommended Posts

hi all

 

trying to use th ADC on the pic16F876A a little confused as to how to use the ADC functionality with source boost :(

anyone with any help on this

#include <system.h>

#define OP_PORT_0 porta.4 
#define OP_PORT_1 porta.5
#define IP_PORT_  porta.1

#define TX_PORT				0x07
#define TX_TRIS				0x87
#define TX_BIT				6
#define RX_PORT				0x07
#define RX_TRIS				0x87
#define RX_BIT				7
#define e_SPBRG				0x99
#define e_RCREG				0x1a
#define e_TXREG				0x019
#define e_TXSTA				0x98
#define e_RCSTA				0x18
#define e_TXIF_PIR		0x0c
#define e_RCIF_PIR		0x0c
#define e_TXIF_BIT		4
#define e_RCIF_BIT		5
#define MODE				(USART_reset_wdt | USART_HW)

#define bit_time 260

#include <rs232_driver.h>

void main()
{
// Set porta as digital output port
set_bit ( adcon1, ADFM ); 
set_bit( adcon1, PCFG0 );  
clear_bit ( adcon1, PCFG1 ); 
set_bit ( adcon1, PCFG2 );
set_bit( adcon1, PCFG3 );

// Set up interrupt control
set_bit( intcon, T0IE );
set_bit( intcon, GIE );

// Config timer in option reg
clear_bit( option_reg, T0CS );
clear_bit( option_reg, T0SE );

// Config timer pre-scalar assignments
set_bit ( option_reg, PSA );
clear_bit( option_reg, PS2 );
clear_bit( option_reg, PS1 );
clear_bit( option_reg, PS0 );

// Set timer1
clear_bit( t1con, T1CKPS1 );
clear_bit( t1con, T1CKPS0 );
set_bit ( t1con, TMR1ON );


trisa = 14; 
porta = 0;




// BAUD RATE: 9600
uart_init( 1, (20000000/(16*9600)) );

while(1) {
	if ( getc() ) {
		char adc_val = //ADC?
	}
}

}

Link to post
Share on other sites
trying to use th ADC on the pic16F876A a little confused as to how to use the ADC functionality with source boost :(

 

Yes, thats confused.

 

Read your post again, decide what you are trying to do, and then repost.

Link to post
Share on other sites

Walshy,

 

This is a fragment of program for a PIC16F873 which worked well. I hope you can

get what you need from it.

 

Huck

 

//*************************************************************
//															  *
// Interface to Joystick 									  *
//															  *
//*************************************************************
// 7 Feb 2007 changed data conversion to prevent spillover top and bottom
#include <system.h>
#include "adc.inc"

#pragma DATA 0x2007, _HS_OSC & _WDT_OFF & _LVP_OFF & _BODEN_OFF & _CPD_OFF & _PWRTE_ON
#pragma CLOCK_FREQ 10000000

void output(short left, short right)
{
clear_bit(portc,6);					// send output to "A"
portb = left;
clear_bit(portc,7);					//	
delay_10us(1);						// toggle "write" input
set_bit(portc,7);					//
set_bit(portc,6);					// send output to "B"
portb = right;
clear_bit(portc,7);					//
delay_10us(1);						// toggle "write" input	
set_bit(portc,7);					//
}
void main()
{
bit fault;
char loop;
short leftval,rightval,steerval,temp;
signed short deflection,steer,left,right,diff;

trisa = 0x3f;						// all inputs
trisb = 0x00;						// all outputs
trisc = 0x00;						// all outputs
adcon0 = 0x81;						// initialize controller
adcon1 = 0x82;						// all analog; Vdd and Vss references
set_bit(portc,6);
set_bit(portc,7);
fault = 0;

//*****************************************************************	
//	Begin interface functions									 *
//*****************************************************************
output (128,128);
for (loop = 20; loop > 0; loop --){
	clear_bit(portc,6);
	delay_ms(25);
	set_bit(portc,6);
	delay_ms(25);
	}
while(1){
	leftval = adc_measure(0);			// acquire inputs
	delay_10us(2);
	rightval = adc_measure(1);
	delay_10us(2);
	clear_bit(portc,6);				
	steerval = adc_measure(2);
	if((leftval > 800)|(leftval < 200)){	
		output(128,128);				
		while(1){}
		}

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