The registers of the ADC are read,
int Adc_read (void)
{
int result;
#if adstart==0
result = ADC. adcdat0&0x3ff;
while (!) ( Adc. adccon& (0x1<<15)))
{
}
#elif adstart==1
while (!) ( Adc. adccon& (0x1<<15)))
{
}
result = ADC. adcdat0&0x3ff;
#endif
return 3300/0x3ff*result;
}
#endif
**************************************
void Niuniu (void)
{
Uart_init ();
Adc_init ();
Beeper_init ();
Adc_read ();
while (1)
{
Itoa (Adc_read ());
if (Adc_read () > 2500)
{
Beeper_on ();
}
Else
{
Beeper_off ();
}
Delay (10000);
}
}
#endif
**********************************
The registers of the ADC are read in decimal type int. , there are decimals, so convert to thousands.
In order to print out so use the itoa, compared with the time not live converted to string adc_read () > 2500;
While the RTC registers in the time with 16 binary, Atoh string into int type 16, we usually use time than into 22:19:54 is the decimal expression, I said the number is not his carry, we set the time of two ways:
Method 1:char time[]={0x22,0x19,0x54};
Char time[]={34,25,84};
Method 2: Use the serial port, Timeset 22:19:54 (the string using strtok cut out to put in time)
Time[0] = Atoh (p[1]);
TIME[1] = Atoh (p[2]);
TIME[2] = Atoh (p[3]);
TIME[3] = Atoh (p[4]);
TIME[4] = Atoh (p[5]);
TIME[5] = Atoh (p[6]);
TIME[6] = Atoh (p[7]);
The Atoh function appears to convert the string to the 16 binary of int, not actually
int Atoh (char *p)
{
int sum = 0;
while (*p! = ')
{
sum = sum*16+*p++-' 0 ';
}
Return sum;
}
Example:
String 14 (there is a +)
sum=0*16+ ' 1 ' (ASCII code for character 1)-' 0 ' (ASCII code of character 0)
*p++ to 4 (refers to character 4)
sum=1*16+ ' 4 '-' 0 ' =20
Atoh function, function your own brain to think of 10 binary number 14 o'clock write the character 1 and the character 4, using the Atoh function, the 10 in the brain thinking 14 is the decimal 20. Because you do not add 0x, so you can only write 10 binary. In fact, BCD is only 2 binary encoding, not 16, and our storage 16 in the system, we have to convert 16 into 10 binary (also can be directly 16 in the assignment, must add 0x), without adding 0x to register, this is only to register assignment, how to assign the value does not matter, Only the register is stored in 16 binary, and we read from the register,
Void Rtc_display (void)
{
puts ("\ r \ n");
Htoa (RTC. Bcdyear);
Putc ('-');
Htoa (RTC. Bcdmon);
Putc ('-');
Htoa (RTC. Bcddate);
Putc (");
Htoa (RTC. Bcdday);
Putc (");
Htoa (RTC. Bcdhour);
Putc (': ');
Htoa (RTC. Bcdmin);
Putc (': ');
Htoa (RTC. BCDSEC);
}
The number we read from the register is the 16 binary of int, we use Htoa for conversion,
void Htoa (int h)
{
Putc (h/16+48);
PUTC (h%16+48);
}
This function such as 20 (without ox, but he is 16 binary), we want to print out, must be translated into characters, 20/16+48,=1 (corresponding character ' 0 ')
20%16=4,+48, corresponding to the character 4
so the register is still in the decimal.
*************************************
RTC
Application layer
void Niuniu (void)
{
int time[7];
Char buf[32], *p[9];
int i;
Uart_init ();
while (1)
{
Puts ("\r\nrtc>");
Gets (BUF);
Buf[strlen (BUF)-2] = ' + ';
P[0] = strtok (buf, "");
for (I=1; p[i-1]!=null; i++)
{
P[i] = strtok (NULL, "");
}
if (strcmp (p[0], "timeset") = = 0)
{
Time[0] = Atoh (p[1]);
TIME[1] = Atoh (p[2]);
TIME[2] = Atoh (p[3]);
TIME[3] = Atoh (p[4]);
TIME[4] = Atoh (p[5]);
TIME[5] = Atoh (p[6]);
TIME[6] = Atoh (p[7]);
Rtc_init (time);
}
if (strcmp (p[0], "time") = = 0)
{
Rtc_display ();
}
}
}
**********************************************
void Rtc_init (int *t)
{
Rtc. Rtccon = 0x1;
Rtc. Bcdyear = t[0];
Rtc. Bcdmon = t[1];
Rtc. Bcddate = t[2];
Rtc. Bcdday = t[3];
Rtc. Bcdhour = t[4];
Rtc. Bcdmin = t[5];
Rtc. Bcdsec = t[6];
Rtc. Rtccon = 0x0;
}
void Rtc_display (void)
{
Puts ("\ r \ n");
Htoa (RTC. Bcdyear);
PUTC ('-');
Htoa (RTC. Bcdmon);
PUTC ('-');
Htoa (RTC. Bcddate);
PUTC (");
Htoa (RTC. Bcdday);
PUTC (");
Htoa (RTC. Bcdhour);
PUTC (': ');
Htoa (RTC. Bcdmin);
PUTC (': ');
Htoa (RTC. BCDSEC);
}
#endif
*********************************
#if string==1
void itoa (int i)
{
PUTC (i/1000+48);
PUTC (i%1000/100+48);
PUTC (i%100/10+48);
PUTC (i%10+48);
Puts ("\ r \ n");
}
void Htoa (int h)
{
PUTC (h/16+48);
PUTC (h%16+48);
}
int Atoh (char *p)
{
int sum = 0;
while (*p! = ')
{
sum = sum*16+*p++-' 0 ';
}
return sum;
}
Example:
String 14 (there is one in the rear)
sum=0*16+ ' 1 ' (ASCII code of character 1)-' 0 ' (ASCII 60 of character 0)
*p++ becomes 4 (refers to character 4)
sum=1*16+ ' 4 '-' 0 ' =20
#endif
***********************************
Reading of registers for ADC and RTC