main()
{
int i;
TRISB= 0xFF;
i=0;
j=9;
while (1) {
//wait for press
while (RB6); // loop (1)
DelayMs (30); //debounce
//wait for release
while (!RB6); //loop(2)
DelayMs(30); //debounce
i++;
//wait for press
while (RB4); // loop (1)
DelayMs (30); //debounce
//wait for release
while (!RB4); //loop(2)
DelayMs(30); //debounce
j--;
}
how do i combine those digits to use in another function, shud i use a concatination function?
c=getch();
switch(c)
{
case 0:
{
do something;
break;
} case 1:
{
do something;
break;
}
case 2:
{
do something;
break;
}
}
void MakeMenu(char mnu){
unsigned char tPOS;
tPOS = 0;
switch(mnu){
case 0:
ClearBuff();
LCDStringC((unsigned rom char*)"Settings",0,None,0,2);
LCDStringC((unsigned rom char*)" Time",0,None,2,2);
LCDStringC((unsigned rom char*)" Date",0,None,3,2);
LCDStringC((unsigned rom char*)" Alarm 1",0,None,4,2);
LCDStringC((unsigned rom char*)" Alarm 2",0,None,5,2);
//Check Boxes
LCDRect(16,2,7,7);
LCDRect(24,2,7,7);
LCDRect(32,2,7,7);
LCDRect(40,2,7,7);
//Outline
LCDRect(0,0,95,63);
LCDRect(8,0,95,1);
LCDRect(9,0,95,1);
LCDRect(61,0,95,1);
LCDRect(62,0,95,1);
SendBuff();
break;
case 1:
done = 0;
curPos = 0;
while(!done){
ClearBuff();
done2 = 0;
LCDStringC((unsigned rom char*)"Time",0,None,0,2);
//Outline
LCDRect(0,0,95,63);
LCDRect(8,0,95,1);
LCDRect(9,0,95,1);
LCDRect(61,0,95,1);
LCDRect(62,0,95,1);
SendBuff();
LCDStringB(Time,0,None,4,4);
LCDString((unsigned rom char*)"Set: Hour High",0,None,2,4);
while(!done2){
MyButton = GetADC(0);
if(MyButton == DOWN){
switch(curPos){
case 0:
if(Time[0] == '0')
Time[0] = '1';
else
Time[0] = '0';
break;
case 1:
if(Time[0] > '0')
if(Time[1] < '1')
Time[1] = '0';
else
Time[1] = Time[1] - 1;
else
if(Time[1] < '1')
Time[1] = '9';
else
Time[1] = Time[1] - 1;
break;
case 2:
if(Time[3] == '0')
Time[3] = '5';
else
Time[3] = Time[3] - 1;
break;
case 3:
if(Time[4] == '0')
Time[4] = '9';
else
Time[4] = Time[4] - 1;
break;
case 4:
if(Time[6] == '0')
Time[6] = '5';
else
Time[6] = Time[6] - 1;
break;
case 5:
if(Time[7] == '0')
Time[7] = '9';
else
Time[7] = Time[7] - 1;
break;
case 6:
if(Time[10] == 'A')
Time[10] = 'P';
else
Time[10] = 'A';
break;
}
DelayMS(250);
}
if(MyButton == UP){
switch(curPos){
case 0:
if(Time[0] > '0')
Time[0] = '0';
else
Time[0] = '1';
break;
case 1:
if(Time[0] > '0')
if(Time[1] > '1')
Time[1] = '0';
else
Time[1] = Time[1] + 1;
else
if(Time[1] > '8')
Time[1] = '0';
else
Time[1] = Time[1] + 1;
break;
case 2:
if(Time[3] == '5')
Time[3] = '0';
else
Time[3] = Time[3] + 1;
break;
case 3:
if(Time[4] == '9')
Time[4] = '0';
else
Time[4] = Time[4] + 1;
break;
case 4:
if(Time[6] == '5')
Time[6] = '0';
else
Time[6] = Time[6] + 1;
break;
case 5:
if(Time[7] == '9')
Time[7] = '0';
else
Time[7] = Time[7] + 1;
break;
case 6:
if(Time[10] == 'A')
Time[10] = 'P';
else
Time[10] = 'A';
break;
}
DelayMS(250);
}
if(MyButton == ENTER){
if(curPos == 6){
done = 0xFF;
done2 = 0xFF;
}
curPos++;
switch(curPos){
case 1:
LCDString((unsigned rom char*)"Set: Hour Low ",0,None,2,4);
break;
case 2:
LCDString((unsigned rom char*)"Set: Min High ",0,None,2,4);
break;
case 3:
LCDString((unsigned rom char*)"Set: Min Low ",0,None,2,4);
break;
case 4:
LCDString((unsigned rom char*)"Set: Sec High ",0,None,2,4);
break;
case 5:
LCDString((unsigned rom char*)"Set: Sec Low ",0,None,2,4);
break;
case 6:
LCDString((unsigned rom char*)"Set: AM or PM ",0,None,2,4);
break;
}
DelayMS(250);
}
LCDStringB(Time,0,None,4,4);
MyButton = 0;
}
}
if(Time[10] == 'P')
Hours = DECtoBCDB((Time[0]-0x30),(Time[1]-0x30),'h',1);
else
Hours = DECtoBCDB((Time[0]-0x30),(Time[1]-0x30),'h',0);
Minutes = DECtoBCDB((Time[3]-0x30),(Time[4]-0x30),'m',0);
Seconds = DECtoBCDB((Time[6]-0x30),(Time[7]-0x30),'m',0);
WriteSPI(0x80, Seconds);
WriteSPI(0x81, Minutes);
WriteSPI(0x82, Hours);
break;
}
}
void settingPos(char pos){
//Box 1
LCDLine(16,2,23,9,0);
LCDLine(23,2,16,9,0);
//Box 2
LCDLine(24,2,31,9,0);
LCDLine(31,2,24,9,0);
//Box 3
LCDLine(32,2,39,9,0);
LCDLine(39,2,32,9,0);
//Box 4
LCDLine(40,2,47,9,0);
LCDLine(47,2,40,9,0);
if(pos == 1){
LCDLine(16,2,23,9,1);
LCDLine(23,2,16,9,1);
}
if(pos == 2){
LCDLine(24,2,31,9,1);
LCDLine(31,2,24,9,1);
}
if(pos == 3){
LCDLine(32,2,39,9,1);
LCDLine(39,2,32,9,1);
}
if(pos == 4){
LCDLine(40,2,47,9,1);
LCDLine(47,2,40,9,1);
}
SendBuff();
}
void MakeMenu(char mnu){
switch(mnu){
case 0:
DrawSettings();
break;
case 1:
SetTime();
break;
case 2:
//SetDate();
break;
case 2:
//SetAlarm(1);
break;
case 2:
//SetAlarm(2);
break;
}
}
. and display it onto a LCD???Im using a pic 16f690 and programming in Chow do i combine them into a single digit
for(x=0;x<2;x++){
LCDchar(MyNum[x]);
}
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
void FloatToStr(float , char[]);
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
void home_screen(void);
#define LCD_RS RC0 //LCD RS pin
#define LCD_EN RC1 //LCD EN pin
#define LCD_STROBE() LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0
unsigned char cm10; //
unsigned char cm; //
unsigned int math; // used for voltage calculations
unsigned char i,j,k;
char temp[8];
///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit my design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;};
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
{
scale=floor(0.5+scale/10.0);
d=(unsigned char) (n/scale);
str[f]=d+'0';
n=n-scale*d;
f++;
};
str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
{
n=n*10.0;
d=(unsigned char) n;
str[f]=d+'0';
n=n-d;
f++;
};
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////
/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if XTAL_FREQ <= 2MHZ
do {
DelayUs(996);
} while(--cnt);
#endif
#if XTAL_FREQ > 2MHZ
unsigned char p;
do {
p = 4;
do {
DelayUs(250);
} while(--p);
} while(--cnt);
#endif
}
void DelayS(unsigned char cnt)
{
for (j=0; j<(cnt*10); j++)
DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////
//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
DelayMs(20); //wait for LCD to be ready
LCD_RS = 0; //write instruction
PORTB = (c & 0xF0); //load upper nibble on LCD data lines
LCD_STROBE(); //send instruction to LCD
PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
LCD_STROBE(); //send instruction to LCD
}
/* send data to the LCD */
void lcd_data(unsigned char c)
{
DelayMs(20); //wait for LCD to be ready
PORTB = 0x00;
LCD_RS = 1; //write data
PORTB |= (c & 0xF0); //load upper nibble on LCD data lines
LCD_STROBE(); //send instruction to LCD
PORTB &= 0x00; //load upper nibble on LCD data lines
PORTB |= ( (c << 4) & 0xF0);
LCD_STROBE(); //send instruction to LCD
}
/*Clear the LCD*/
void lcd_clear(void)
{
lcd_cmd(0x01); //command to clear LCD
}
[B]/*write a string of chars to the LCD*/[/B]
void lcd_puts(const char s[])
{
j = -1;
while(s[++j]!=('\0')) // send characters until null character reached
lcd_data(s[j]);
}
/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
lcd_cmd(0b10000000); // command to go to line 1
}
/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
lcd_cmd(0b11000000); // command to go to line 2
}
/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
lcd_cmd(((x)&0x7F)|0x80);
}
/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
LCD_RS = 0;
LCD_EN = 0;
DelayMs(20); //wait for LCD startup
lcd_cmd(0x28); // 4-bit mode
lcd_cmd(0x08); // display off
lcd_cmd(0x01); // clear display
lcd_cmd(0x0C); // disp. on, cursor off, cursor blink off
lcd_cmd(0x06); // entry mode
lcd_cmd(0x80); // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
void init(void)
{ OSCCON = 0x72; // internal osc, 8MHz
PORTA = 0;
TRISA = 0b10000010; // RA7 high imp, RA3 is serial out,
// RA1 is ADC input measuring VR1
PORTB = 0;
WPUB = 1;
RABPU = 0;
// INTCON2 = 0; // PORTB pullups ON
TRISB = 0b00000000; // PORTB not used
ADCON0 = 0b00000101; // ADC ON, RA1 is ADC input
ADCON1 = 0b01111101; // AN1 is ADC input
// ADCON2 = 0b10100010; // right justify, 8Tad, 32Tosc
T1CON = 0b00010001; // TMR1 is ON, 1:2 prescale, =1MHz
// T3CON = 0b00010001; // TMR3 is ON, 1:2 prescale, =1MHz
TRISC=0xFC;
ANSEL=0x02;
ANSELH=0x00;
lcd_init();
GIE=0;
PORTA = 0x00;
}
void home_screen(void)
{
lcd_clear();
lcd_goto_L1();
lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
lcd_goto_L2();
lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
}
void calc_distance(void)
{
// from the transeiver datasheet the analog voltage is
// the inverse of distance, so distance can be calculated
// d = (1 / volts) then just scaled to suit the transeiver
// load ADC value in 16bit math var
math = ADRESH;
math = (math * 256);
math += ADRESL;
// now invert it; (1 / volts) use (6050 / volts) for scaling
math = (6050 / math);
if(math >= 2) math -=2; // fix linear error (-2)
if(math > 99) math = 99; // max limit at 99cm
// convert from 0-99 to 2 decimal digits, 0-99cm
cm10=0;
while(math >= 10)
{
cm10++;
math -= 10;
}
cm = math;
}
void main(void)
{
init(); // initialise I/O ports, LCD
while (1)
{
home_screen();
}
}
since these are decimals you need ascii so you would do:
/* ---------------------------------------------
ex:
ShowDigits(3,5);
Displays 2 digit data to LCD
--------------------------------------------- */
void ShowDigits(unsigned char var1,unsigned char var2){
var1 += 0x30;
var2 += 0x30;
lcd_data(var1);
lcd_data(var2);
}
;#############################################################################
;# VAULT INFORMATION SERVICES - 8052 CODE EXAMPLE #
;# (C) Copyright 1997 by Vault Information Services #
;# #
;# This code fragment is provided to the public free of charge and WITHOUT #
;# OF ANY KIND, NEITHER EXPRESS OR IMPLIED. This code is offered as an #
;# example of 8052 programming. VIS extends no warranty regarding its #
;# functionality or fitness for a specific purpose.;# #
;# The user may utilize this code in any way he/she sees fit, including #
;# using it in his/her own program, commercial or otherwise. However, by #
;# using this code the user states his/her acceptance of the above and #
;# holds VIS indemnified against any and all claims that may arise due to #
;# the use or misuse of this code. #
;#############################################################################
;********************************************************************
;********************************************************************
;** Function: bcd_to_byte **
;** Purpose: Convert a two-byte BCD into a single byte **
;** Input: A = Hi byte to convert (ASCII 0x30-0x39,0x41-0x46)**
;** R0 = Lo byte to convert (ASCII 0x30-0x39,0x41-0x46)**
;** Output: A = Converted value (binary 0x00-0xFF) **
;** Destroyed Registers: None **
;********************************************************************
;********************************************************************
bcd_to_byte:
XCH A,R0
SUBB A,#30h
JNB ACC.4,bcd_to_byte_2
SUBB A,#07h
bcd_to_byte_2:
XCH A,R0
SUBB A,#30h
JNB ACC.4,bcd_to_byte_3
SUBB A,#07h
bcd_to_byte_3:
SWAP A
ORL A,R0
RET
unsigned char Combine(unsigned char SinglePOS, unsigned char TenPOS){
unsigned char wholeNum;
wholeNum = SinglePOS + (TenPOS * 10);
return wholeNum;
}
NumDec = Combine(NumSep[0],NumSep[1]);
ShowDigits(NumSep[0],NumSep[1]);
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
unsigned char NumDec;
unsigned char NumSep[2];
unsigned char i,j,k;
char temp[8];
int height=50, SensorPos=10;
char input_sw;
#define HOME_SW RC2 //HOME switch
#define INCREASE_SW RC3 //INCREASE switch
#define DECREASE_SW RC4 //DECREASE switch
#define ENTERSETTINGS_SW RA4 //ENTERSETTINGS switch
void read_input_sw(void)
{
char input_sw2;
input_sw = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1)); //create a binary combination of the input switches
if (input_sw != 0) //check if any of the switches are pressed
{
DelayMs(20); //switch debouncing
input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
while (input_sw2 != 0) //if a switch is pressed wait for release
{
input_sw2 = ((HOME_SW*8)+(INCREASE_SW*4)+(DECREASE_SW*2)+(ENTERSETTINGS_SW*1));
DelayMs(20); //switch debouncing
}
}
}
void home_screen(void)
{
lcd_clear();
lcd_goto_L1();
lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
lcd_goto_L2();
lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
}
//ShowDigits(3,5);
// Displays 2 digit data to LCD
//---------------------------------------------
void ShowDigits(unsigned char var1,unsigned char var2){
var1 += 0x30;
var2 += 0x30;
lcd_data(var1);
lcd_data(var2);
}
//turn 2 digits from user into a single number (decimal) like
//Decimal 3 and 5 into 35(which is 0x23 in hex).
unsigned char Combine(unsigned char SinglePOS, unsigned char TenPOS){
unsigned char wholeNum;
wholeNum = SinglePOS + (TenPOS * 10);
return wholeNum;
}
int enter_settings(void)
{ int Var1=0;
int Var2=9;
do
{ read_input_sw();
switch (input_sw)
{
case 8: goto a;
case 4: { Var1++; //incrementing 1st digit
NumSep[0]=Var1++;
break;
}
case 2: { Var2--; //decrementing second digit
NumSep[1]=Var2;
break;
}
default : goto a; //should any abnormalties occur
}
ShowDigits(NumSep[0],NumSep[1]);
}
while (input_sw==1); //user presses enter/settings button when satisfied
NumDec = Combine(NumSep[0],NumSep[1]);
return NumDec;
a:
return;
}
void menu(void)
{
read_input_sw();
while ((input_sw != 8)&(input_sw != 1)) //Wait for Home or enter/settings
{
lcd_clear();
lcd_goto_L1();
lcd_puts(" ENTER/SETTINGS ");
DelayS(1);
lcd_clear();
ShowDigits(NumSep[0],NumSep[1]);
lcd_clear();
lcd_goto_L1();
lcd_puts(" ENTER HEIGHT ");
height=enter_settings();
lcd_clear();
lcd_goto_L1();
lcd_puts(" ENTER SENSOR POSITION ");
SensorPos=enter_settings();
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?