Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/10/05 06:00
Modified:
  10/10/05 06:25

Read: times


 
#102147 - How to handle interrupts in this code
Hi all,
Iam working c8051f005 controller,iam performing A/D conversion and displaying it on hyperterminal.There are five A/D channels are there.Iam performing A/Dconversion by using A/D interrupt.Now iam trying to add another interrupt ie INT0 by using switch , whenever user presses switch iwant to display 0 to 4 A/D channels.but currently its showing only one channel display result.how to display 0 to 4 channels when user presses switch.your ideas are welcome.

Best regards
Raghav

Code

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <c8051f000.h> // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for \'F00x
//-----------------------------------------------------------------------------

sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK 16000000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SAMPLE_RATE 10000 // Sample frequency in Hz
#define VREF0 2430 // VREF voltage in millivolts
sbit LED = P1^6; // LED=\'1\' means ON
sbit SW = P1^7;
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void keypressed(void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
int is_key_pressed;
long result[1];
int i=0;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) {

long voltage; // voltage in millivolts
// loop counter

WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;

SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0


Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate

ADC0_Init (); // init ADC

ADCEN = 1; // enable ADC


IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 ()
EX0 = 1; // Enable EX0 Interrupt
EA = 1; // Enable Global Interrupt Flag


while(1)
{

is_key_pressed = 0;

if(is_key_pressed)

{

is_key_pressed = 0;

EA = 0; // disable interrupts
voltage = result[i]; // get ADC value from global variable
EA = 1; // re-enable interrupts

voltage = voltage * VREF0;
voltage = voltage >> 16;
printf (\"Channel \'%d\' voltage is %ldmV\\n\",i, voltage);




}


}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------

void SYSCLK_Init (void)
{
// int i; // delay counter

//OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal

//for (i=0; i < 256; i++) ; // wait for oscillator to start

//while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle

OSCICN = 0x87; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
XBR0 |= 0x04; // Enable UART0
//XBR1 |= 0X04;
XBR2 |= 0x40; // Enable crossbar and weak pull-ups
PRT0CF |= 0x01; // enable TX0 as a push-pull output
PRT1CF |= 0x40; // enable P1.6 (LED) as push-pull output
//PRT1CF |=0X80;

}

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
SCON = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = 0xf6;//-(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1 (disable baud rate
// divide-by-two)
TI = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------


void ADC0_Init (void)
{
ADC0CN = 0x05; // ADC0 disabled; normal tracking
// mode; ADC0 conversions are initiated
// on overflow of Timer3; ADC0 data is
AMX0CF = 0X00; // left-justified
REF0CN = 0x03; // disable temp sensor on-chip VREF,
// and VREF output buffer
AMX0SL = 0x00; // Select TEMP sens as ADC mux output
ADC0CF = 0x80; // ADC conversion clock = SYSCLK/16
ADC0CF|= 0x00;
EIE2|= 0x02; // enable ADC interrupts
}
/////////////////////////////////////////////////
//-----------------------------------------------------------------------------
// Timer3_Init
//-----------------------------------------------------------------------------

void Timer3_Init (int counts)
{
TMR3CN = 0x02; // Stop Timer3; Clear TF3;
// use SYSCLK as timebase
TMR3RL = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 &= ~0x01; // disable Timer3 interrupts
TMR3CN |= 0x04; // start Timer3
}
////////////////////////////////////////////////////////////////////////
void ADC0_ISR (void) interrupt 15
{

static unsigned int channel = 0; // ADC mux channel (0-5)

LED =1;

ADCINT = 0;

result[channel] = ADC0;
channel++;
if (channel == 5)
{
channel = 0;

}

AMX0SL = channel;
LED =0;

}
///////////////////////////////////////////
void Keypressed(void) interrupt 0
{


LED=1; //when swicth pressed iam trying to
update the i value so that all the channels are displayed
is_key_pressed = 1;
if((i!=0) && (i<5))
{
i++;
}


}

List of 7 messages in thread
TopicAuthorDate
How to handle interrupts in this code            01/01/70 00:00      
   what about i?            01/01/70 00:00      
      handling interrupts.            01/01/70 00:00      
         the dangers of 'i'            01/01/70 00:00      
         debounce switch            01/01/70 00:00      
            errare humanum...            01/01/70 00:00      
               Using interrupts with switches            01/01/70 00:00      

Back to Subject List