1230
Member level 3
hello...
i am using STM8s003f3 to read 2 voltages from 2 different channels.
but my readings are fluctuating. cann't understand the problems. i think i have done mistake in registors. pls guide .
for testing , have given ADC voltage from a 1.2v cell.
i am quoting some part of code which is related to ADC.
i am using STM8s003f3 to read 2 voltages from 2 different channels.
but my readings are fluctuating. cann't understand the problems. i think i have done mistake in registors. pls guide .
for testing , have given ADC voltage from a 1.2v cell.
i am quoting some part of code which is related to ADC.
Code:
#include <iostm8s003f3.h>
#include "stm8s.h"
unsigned int AD_Value,TIMER4 = 0;
unsigned long int VIR,VOR,VI,VO;
int main(void)
{
CLK_ICKR |= (1<<0); //HSIEN=1 //HSI Oscilator Enable
// CLK_CKDIVR &=~((1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0));
// CLK_CKDIVR |= (1<<4)|(1<<3); //Fmaster = 2MHz. Default clock - 16MHz, Prescaler - 8
//CLK_CKDIVR |= (1<<0); //CPU Clock = Fmaster/2 - Prescaler selected 2
ADC_INIT();
PD_ODR |=((1<<4)|(1<<5)|(1<<6));
INITTIMER4();
while(1)
{
ADC();
}
}
int ADC()
{
DELAY(1);
AV=VIR=VOR=0;
while(AV<=1999)
{
VOR=VOR+(READADC(4));
AV++;
VIR=VIR+(READADC(3));
}
VIR=(VIR/2000) ;
VOR=(VOR/2000) ;
VO=VOR;
VI=VIR;
return 0;
}
void ADC_INIT()
{
ADC_CR2 &=~(1<<1);
ADC_CR1 |=(1<<1);
ADC_CR1 &=~((1<<6)|(1<<4)|(1<<5));
ADC_CR2 &= ~(1<<3);
ADC_CR2=0;
ADC_CR3=0;
ADC_CR1 &= ~((1<<4)|(1<<5)|(1<<6));
ADC_CR1 |= (1<<0); // ADC ON
}
unsigned int READADC(unsigned char CHANNEL)
{
AD_Value =0;
ADC_CSR = ((0x0F)&CHANNEL); //Channel Selected
ADC_CR1 |= (1<<0); //ADON -> 1
while(!(ADC_CSR&(1<<7))); //Wait while EOC is 0 | while ADC is running
AD_Value =((((unsigned int)ADC_DRH)<<2)+ADC_DRL); //ADC Value copied in AD_Value
ADC_CSR &= ~(1<<7);
return AD_Value;
}
void INITTIMER4()
{
TIM4_CR1 |=0x01; //Enable timer
CLK_PCKENR1 |=(1<<4); //Enable peripheral clock for timer 4
TIM4_PSCR |=0X0a; //clock prescaler, divide by max
TIM4_IER |=0x01; //enable update interrupt
asm("rim"); //Enable global interrupt
}
#pragma vector=0x19
__interrupt void TIM4_UPD_OVR_IRQHANDLER(void)
{
if(TIMER4 > 100)
{
DISP(VI); // this function is only displaying on seven segment
TIMER4=0;
}
TIMER4++;
TIM4_SR &=~(1<<0);
}