Thunderhorse312
Member level 1
Hi can somebody show me a C program for a digital weighing scale.. I tried to program one but i wasn't able to receive the result that i expect... here is my code
I was trying to open an output pin after a corresponding input analog voltage is present.. Is my code right? please post here about mistakes in the program.. Thanks!
Code:
#include <htc.h>
#include <pic16f877a.h>
#include "delay.c"
#define _XTAL_FREQ 16000000
unsigned int ADCRead(unsigned char channel)
{
unsigned char l_byte, h_byte; //Variables in exchange for ADRESL and ADRESH
unsigned int ADR; //stores the Analog to Digital input value
ADCON0 = 0x81; //sets what channel to be used for analog input
DelayUs(50); //acquistion time delay
GO_DONE = 1; //start the A/D conversion
while(GO_DONE == 1); //wait for the bit to be cleared
l_byte = ADRESL; //set l_byte as ADRESL
h_byte = ADRESH; //set h_byte as ADRESH
ADR = (h_byte << 8)|l_byte; //shift ADRESH 8 places to the left and be ORed with ADRESL
GO_DONE = 0;
return ADR;
}
void main(void)
{
#define Weight25 100 //469
#define Weight30 150 //474
#define Weight31 200 //475
#define Weight35 250 //479
#define Weight36 300 //480
#define Weight40 350 //484
#define Weight41 400 //485
#define Weight46 450 //490
#define Weight47 500 //491
#define Weight52 550 //496
#define Weight53 600 //497
#define Weight58 650 //502
#define Weight59 700 //503
#define Weight64 750 //508
#define Weight65 800 //509
#define Weight70 850 //514
#define Weight71 900 //515
#define Weight76 950 //520
#define Weight77 1000 //521
#define Weight82 526
#define Weight83 527
#define Weight88 532
#define Weight89 533
#define Weight94 538
// #define Weight95 539
// #define Weight100 544
unsigned int Ch0;
PORTD = 0;
TRISD = 0;
PORTB = 0;
TRISB = 0;
ADCON0 = 0x81;
ADCON1 = 0xC0;
while (1)
{
Ch0 = ADCRead(0); //Read from channel 0
if (Ch0 >= Weight25)
{
if (Ch0 <= Weight30)
{
RB0 = 1;
}
else
{
RB0 = 0;
}
}
else
{
RB0 = 0;
}
if (Ch0 >= Weight31) //weight 31 - 35
{
if (Ch0 <= Weight35)
{
RB1 = 1;
}
else
{
RB1 = 0;
}
}
else
{
RB1 = 0;
}
if (Ch0 >= Weight36) //weight 36 - 40
{
if (Ch0 <= Weight40)
{
RB2 = 1;
}
else
{
RB2 = 0;
}
}
else
{
RB2 = 0;
}
if (Ch0 >= Weight41) //weight 41 - 46
{
if (Ch0 <= Weight46)
{
RB3 = 1;
}
else
{
RB3 = 0;
}
}
else
{
RB3 = 0;
}
if (Ch0 >= Weight47) //weight 47 - 52
{
if (Ch0 <= Weight52)
{
RB4 = 1;
}
else
{
RB4 = 0;
}
}
else
{
RB4 = 0;
}
if (Ch0 >= Weight53) //weight 53 - 58
{
if (Ch0 <= Weight58)
{
RB5 = 1;
}
else
{
RB5= 0;
}
}
else
{
RB5 = 0;
}
if (Ch0 >= Weight59) //weight 59 - 64
{
if (Ch0 <= Weight64)
{
RB6 = 1;
}
else
{
RB6 = 0;
}
}
else
{
RB6 = 0;
}
if (Ch0 >= Weight65) //weight 65 - 70
{
if (Ch0 <= Weight70)
{
RD0 = 1;
}
else
{
RD0 = 0;
}
}
else
{
RD0 = 0;
}
if (Ch0 >= Weight71) //weight 71 - 76
{
if (Ch0 <= Weight76)
{
RD1 = 1;
}
else
{
RD1 = 0;
}
}
else
{
RD1 = 0;
}
if (Ch0 >= Weight77) //weight 77 - 82
{
if (Ch0 <= Weight82)
{
RD2 = 1;
}
else
{
RD2 = 0;
}
}
else
{
RD2 = 0;
}
if (Ch0 >= Weight83) //weight 83 - 89
{
if (Ch0 <= Weight89)
{
RD3 = 1;
}
else
{
RD3 = 0;
}
}
else
{
RD3 = 0;
}
// if (Ch0 >= Weight89) //weight 89 - 94
// {
// if (Ch0 <= Weight94)
// {
// RD4 = 1;
// }
// else
// {
// RD4 = 0;
// }
//
// }
// else
// {
// RD4 = 0;
// }
//
//
// if (Ch0 >= Weight95) //weight 95 - 100
// {
// if (Ch0 <= Weight100)
// {
// RD5 = 1;
// }
// else
// {
// RD5 = 0;
// }
//
// }
// else
// {
// RD5 = 0;
// }
}
}
I was trying to open an output pin after a corresponding input analog voltage is present.. Is my code right? please post here about mistakes in the program.. Thanks!