necati
Joined: 12 Sep 2003 Posts: 37 Location: istanbul
|
power lcd |
Posted: Sun Nov 09, 2008 4:16 am |
|
|
Code: | #include <16f873.h>
#device ADC=10 //volt lcd 5/5/2008
#fuses XT, NOPROTECT,PUT, NOWDT,BROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7)
#BYTE PORT_A=0X05
#BYTE PORT_B=0X06
#BYTE PORT_C=0X07
int16 volt,volt0,volt1,wat1;
int16 v,v0,v1,gvmax,avmax;
float data=0;
float ka=200.0/1024,kv=50.0/1024;
////////////////////////////////////
void charge(void);
void aku(void);
void mesure(void);
/***[email protected]***/
int16 duty=0;
int8 sec=50,sn=0,mn=0;
#int_timer0
ms_10(){
set_timer0(70); //(255-60)*128*20= 256ms
if(sec){
sec--;}
else{sec=50;
sn++;if(sn==60) {mn++;sn=0;}
}}
/***[email protected]***/
// LCD STUFF
#define LCD_RS PIN_b6
#define LCD_EN PIN_b5
#define LCD_D4 PIN_b4
#define LCD_D5 PIN_b3
#define LCD_D6 PIN_b2
#define LCD_D7 PIN_b1
#define FIRST_LINE 0x00
#define SECOND_LINE 0x40
#define CLEAR_DISP 0x01
#define CURS_ON 0x0e
#define CURS_OFF 0x0c
/***[email protected]***/
#use fast_io ( a )
#use fast_io ( b )
#use fast_io ( c )
/***[email protected]***/
// proto statements
void LCD_Init ( void );
void LCD_SetPosition ( unsigned int cX );
void LCD_PutChar ( unsigned int cX );
void LCD_PutCmd ( unsigned int cX );
void LCD_PulseEnable ( void );
void LCD_SetData ( unsigned int cX );
/***[email protected]***/
void main(void){
setup_adc_ports(all_analog);
setup_adc(ADC_CLOCK_INTERNAL);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
///////////////////////////////////////
setup_timer_2(T2_div_by_16,128,1); //freq=4000000/4*1*128=500hz
setup_ccp1(ccp_pwm);
set_pwm1_duty(0);
///////////////////////////////////////
enable_interrupts(int_timer0);
enable_interrupts(global);
SET_TRIS_A (0b11111111);
SET_TRIS_B (0b10000001);
SET_TRIS_C (0b10000011);
PORT_A=0;
PORT_B=0;
PORT_C=0;
lcd_init();
LCD_PutCmd(CLEAR_DISP);
LCD_SetPosition (first_LINE+0);
printf (LCD_PutChar, "POWER METER " );
LCD_SetPosition (second_LINE+0);
printf (LCD_PutChar, "50volt 200amper" );
delay_ms(100);
///////////////////////////////////////////////
while(1){
if(sn>2){aku();sn=0;}
}}
/////////////////////////////////////////////
void aku(void){
mesure();
LCD_PutCmd(CLEAR_DISP);
/////////////////////////////////////
LCD_SetPosition(first_LINE+0);
printf(LCD_PutChar,"AKU " );
/////////////////////////////////////
data = (float)v0*kv ;
LCD_SetPosition(first_LINE+4);
printf(lcd_putchar,"\%02.1fV ",data);
/////////////////////////////////////
data = (float)v1*ka;
LCD_SetPosition(first_LINE+11);
printf(lcd_putchar,"\%02.1fA ",data);//aku volt
/////////////////////////////////////
printf("duty=%ld,volt=%ld\n\r",duty,avmax);
//////////////////////////////////////ruz akim
LCD_SetPosition(second_LINE+0);
printf(LCD_PutChar,"POW " );
//////////////////////////////////////ruz akim
data =(float)(v0*kv)*(v1*ka);
LCD_SetPosition(second_LINE+4);
printf(lcd_putchar,"\%01.0fw ",data);
//////////////////////////////////////wat1
}
//////////////////////////////////////
/***[email protected]***/ //lcd basla
void LCD_Init ( void ){
LCD_SetData ( 0x00 );
output_low ( LCD_RS );
LCD_SetData ( 0x03 ); // init with specific nibbles to start 4-bit mode
LCD_PulseEnable();
LCD_PulseEnable();
LCD_PulseEnable();
LCD_SetData ( 0x02 ); // set 4-bit interface
LCD_PulseEnable(); // send dual nibbles hereafter, MSN first
LCD_PutCmd ( 0x2C ); // function set (all lines, 5x7 characters)
LCD_PutCmd ( 0x0C ); // display ON, cursor off, no blink
LCD_PutCmd ( 0x01 ); // clear display
LCD_PutCmd ( 0x06 ); // entry mode set, increment
}
/***[email protected]***/
void LCD_SetPosition ( unsigned int cX )
{
// this subroutine works specifically for 4-bit Port A
LCD_SetData ( swap ( cX ) | 0x08 );
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) );
LCD_PulseEnable();
}
/***[email protected]***/
void LCD_PutChar ( unsigned int cX )
{
// this subroutine works specifically for 4-bit Port A
output_high ( LCD_RS );
LCD_SetData ( swap ( cX ) ); // send high nibble
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) ); // send low nibble
LCD_PulseEnable();
output_low ( LCD_RS );
}
/***[email protected]***/
void LCD_PutCmd ( unsigned int cX )
{
// this subroutine works specifically for 4-bit Port A
LCD_SetData ( swap ( cX ) ); // send high nibble
LCD_PulseEnable();
LCD_SetData ( swap ( cX ) ); // send low nibble
LCD_PulseEnable();
}
/***[email protected]***/
void LCD_PulseEnable ( void )
{
output_high ( LCD_EN );
delay_us ( 100 );
output_low ( LCD_EN );
delay_ms ( 5 );
}
/***[email protected]***/
void LCD_SetData ( unsigned int cX )
{
output_bit ( LCD_D4, cX & 0x01 );
output_bit ( LCD_D5, cX & 0x02 );
output_bit ( LCD_D6, cX & 0x04 );
output_bit ( LCD_D7, cX & 0x08 );
}
/***[email protected]***/ //lcd son
void mesure(void){
set_adc_channel(0);//voltage
delay_us(25);
v0= read_adc();
volt0= v0*kv;
//////////////////////////
set_adc_channel(1);//current
delay_us(25);
v1= read_adc();
volt1= v1*ka;
wat1=volt0*volt1; //watt
}
/***[email protected]***/ |
|
|