Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] pic 16f887 average value problems

Status
Not open for further replies.

cutu

Newbie level 6
Newbie level 6
Joined
Dec 24, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,363
Code:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned int q=0,*q1[20],frekfencija=30;          // 19531=1hz=1s
unsigned int txt[4],txt1[7],txt2[7],prikaz=0;
unsigned int nula;
unsigned int trenutno,kas=100;
unsigned int raz,sum;
unsigned int j;
unsigned int temp_res,dsp;
char i;

void interrupt(){
 if(pir1.tmr2if){
  pir1.tmr2if=0;
   nula++;
    tmr2=0;
  }
    if (nula==frekfencija){
      nula=0;
     }
    }
void def_interupta(void){
 gie_bit=1;
  peie_bit=1;
  pie1.tmr2ie=1;
  tmr2on_bit=1;
}
void def_ulaza(void){
  TRISA  = 255;              // PORTA is input
  trisc.rc0= 0;                 // PORTC is output
  trisc.rc1= 0;
  TRISB  = 0;                 // PORTB is output
  portc=0;
  porta=0;
  portb=0;
}
void def_lcd(void){
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);
   }
void main() {
  ANSEL  = 0x04;              // Configure AN2 pin as analog
  ANSELH = 0;                 // Configure other AN pins as digital I/O
  C1ON_bit = 0;               // Disable comparators
  C2ON_bit = 0;
  def_interupta();
  delay_ms(500);
  def_ulaza();
  def_lcd();
  *q1=ADC_Read(0);

  
 while(1){
  
        for(i=0;i<9;i++)
         sum+=q1[i];
raz=sum/10;
    wordtostr(raz,txt);
    lcd_out(1,1,txt);



   }

}

- - - Updated - - -

hello.
I need to calculate the average value on analog input.The hardvear is good but I can't get the code working.
I even thried to use interrups to take sample in 1s but I didn't give me resoult.
On the display is writen one number all the time(for example 33).

pic 16f887
20MHz
Mikroc v.5.6.1
 

*q1 = adc_read(0) is not pointer or array of pointers initialization. A pointer expects an address as its value. q1 = &someintvariable; *q1 = adc_read(0) means you are dereferencing q1 and telling that to what address q1 is pointing to, to that address store the value of adc_read(0) functions return value. Don't use pointer instead use a float variable and assign your adc_read(0) value to that and then use FloatToStr() and display on lcd. If you want to take 10 samples then use a float array of 10 elements and take the average of the values.
 
Last edited:
  • Like
Reactions: cutu

    cutu

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top