Code:
// Lcd pinout settings
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D4 at RC2_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D6 at RC4_bit;
sbit LCD_D7 at RC5_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
//########################## globale Variablen ##################### +
char txt1[] = "Druckeransteu.";
unsigned short menue = 0;
unsigned int sollwert1,zaehler=0;
//########################## Funktionsdekleration ##################
unsigned short rotary();
//unsigned short anzeige(unsigned int);
unsigned int eprom_lesen(unsigned short);
unsigned short eprom_schreiben(unsigned short,unsigned int);
//################## Funktionen #########################
void anzeige(unsigned int x)
{
unsigned short anzeige[10] ;
IntToStr(x,anzeige);
Lcd_Out(2,1,anzeige);
// LCD_Chr_CP(46);
// ch = x % 10;
// LCD_Chr(2,12,48+ch);
}
//--------------------------------------------------------
unsigned short rotary_stell(unsigned short adresse) //Sollwert einstellen
{
unsigned short wert1, wert2, e = 1, anzeige[10] ;
unsigned int i ;
if (adresse == 0x00 ) {i = eprom_lesen(adresse) ;}
// if (adresse == 0x02 ) {i = eprom_lesen(adresse) ;}
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"Sollstueckzahl");
Lcd_Out(2,3,"+/-");
IntToStr(i,anzeige);
Lcd_Out(2,6,anzeige);
while ( e == 1 )
{
wert1 = (PORTB & 0x18); //aktuelle stellung nach wert1
if ((wert1 ^ wert2) >= 1) //wurde der Rotary bewegt oder wert1 = wert2
{
wert2 = (wert2 << 1 ); //verschieben des zweiten aufgenommenden wertes
if ((wert1 ^ wert2) == 0x10) {i ++;}
if ((wert1 & wert2) == 0x10) {i -- ;}
if (i > 200) {i = 1 ;}
if (i < 1) {i = 200 ;}
wert2 = wert1 ;
IntToStr(i,anzeige);
Lcd_Out(2,6,anzeige);
}
if (Button(&PORTB, 5, 20, 1) && e == 1)
{
while((PORTB & 0x20)==0x20);
e =0;
if (adresse == 0x00 ) {eprom_schreiben(adresse,i );}
// if (adresse == 0x02 ) {eprom_schreiben(adresse,i );}
LCD_Cmd(_LCD_CLEAR);
delay_ms(600);
LCD_Out(2,6,anzeige);
LCD_Out(2,8,"/");
}
}
return e;
}
//----------------------------------------------------------
unsigned int eprom_lesen(unsigned short adr)
{
int zahl;
unsigned short temp;
temp = Eeprom_Read(adr);
zahl = temp;
zahl = zahl <<8;
temp = Eeprom_Read(adr+1);
zahl = zahl + temp;
return zahl;
}
unsigned short eprom_schreiben(unsigned short xxx,unsigned int xxxx )
{
unsigned int temp;
temp = xxxx;
Eeprom_Write ( xxx+1,temp );
temp = (xxxx>>8);
Eeprom_Write ( xxx,((short)temp) );
return 0;
}
//####################### main #######################
void main()
{
unsigned short enter=0 ;
//unsigned long ;
unsigned int istwert1;
char sollwert2[7],istwert2[7];
TRISA = 0x3F; //Eingang
TRISB = 0x38; //Ausgang und Eingang 00111000
TRISC = 0x00; //Ausgang
PORTA = 0x00; //Alle auf 0
PORTB = 0x00; //Alle auf 0
PORTC = 0x00; //Alle auf 0
INTCON = 0x00;
OPTION_REG = 0x80 ;
PIE1 = 0x00 ;
PIR1 = 0x00 ;
CCP1CON = 0x00 ;
ADCON1 = 0x06; //alle Digital
Lcd_Init(); // Initialize LCD
delay_ms(200);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,2,txt1);
delay_ms (2000);
Lcd_Cmd(_LCD_CLEAR);
sollwert1=eprom_lesen(0x00);
IntToStr(sollwert1,sollwert2);
LCD_Out(1,5,"Aktuell");
LCD_Out(2,6,sollwert2);
LCD_Out(2,8,"/");
istwert1 = eprom_lesen(0x03);
IntToStr(istwert1,istwert2);
LCD_Out(2,1,istwert2);
do
{
if (Button(&PORTB, 5, 20, 1) ) // Menueauswahl
{
while((PORTB & 0x20)==0x20);
enter=0;
menue = rotary_stell(enter); // Sollwert einstellen
LCD_Out(1,5,"aktuell");
anzeige(istwert1);
}
if ((PORTA & 0x02)==0x02)
{
istwert1=istwert1+1;
eprom_schreiben(0x03,istwert1 );
anzeige(istwert1);
while((PORTA & 0x02)==0x02);
}
if (((int)istwert1)==((int)sollwert1))
//res=strcmp(istwert2,sollwert2) ;
//if(res==0)
{
PORTB=PORTB|0x80;
}
else
{
PORTB=PORTB|0x40;
}
}
while(1);
}
Lesezeichen