โจทย์ รับคีย์เข้า INT1 ให้เริ่มแปลงสัญญาณ ADC 10 บิตและแสดงเลขฐานสิบออกที่จอ lcd 20*2 ในบรรทัดแรกและแสดงรหัสนิสิตในบรรทัดที่สอง
และไฟ LED กระพริบเมื่อกดคีย์ (เขียนภาษาซีใน Keil V.3)
#include <REG52.H> /* special function register declarations */
#include <ABSACC.H>
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P17 = P1^7;
sbit P30 = P3^0;
sbit P15 = P1^5;
sbit P35 = P3^5;
sbit P31 = P3^1;
sbit P14 = P1^4;
sbit P26 = P2^6;
sbit P27 = P2^7;
unsigned char name[21];// MEMORY REQUEST 21 BYTE
void delay(int time)
{
int i;
for(i=0;i<time;i++)
{
}
}
void lcd_ena (void)
{
P33=1;
delay(1000);
P33=0;
delay(1000);
P33=1;
}
void wrt_command(unsigned char com)
{
P34 = 0;
P0 = com;
lcd_ena();
}
void set_add(char addr)
{
P34 = 0;
P0 = 0x80|addr;
lcd_ena();
}
void wrt_data(unsigned char dat)
{
P34 = 1;
P0 = dat;
lcd_ena();
}
void show (char *strings)
{
int i,sl;
sl=strlen(strings);
for (i=0;i<sl;i++)
{
wrt_data(strings[i]);
delay(1000);
}
}
void iToBinary(char *out,char *bufs)
{ unsigned int tempo,i;
tempo=atoi(bufs);
for(i=15;i>0;i--)
{
if((tempo%2)==1)
out[i]=1;
else out[i]=0;
tempo=tempo/2;
}
out[0]=tempo;
}
void DAC(char *words)
{
int i;
char st[18];
iToBinary(st,words);
P15=0;
// delay(100);
for(i=0;i<16;i++)
{
P31=st[i];
P30=1;
delay(1);
P30=0;
delay(1);
}
P15=1;
P35=0;
delay(1);
P35=1;
}
int ipow(int base,int power)
{
if(power>0)
{
return base*ipow(base,power-1);
}
else
{
return 1;
}
}
void clear_buffer(unsigned char *str,int num )
{
int i;
for(i=0;i<num;i++)
{
str[i]=0;
}
}
void ADC(unsigned int *data10)
{
unsigned int i;
unsigned char temp[13],bb;
*data10=0;
clear_buffer(&temp,12);
P30=0;
P14=0;
delay(10);
P30=1;
delay(100);
P30=0;
P30=1;
P30=0;
P30=1;
P30=0;
for(i=10;i>0;i--)
{
P30=1;
P27=1;
delay(1);
if(P27)
temp[i]=1;
else
temp[i]=0;
P30=0;
delay(2);
}
for(i=10;i>0;i--)
{
/* if(temp[i]==1)
wrt_data(48+1);
else
wrt_data(48); */
*data10=*data10+(temp[i]*ipow(2,i-1));
}
}
void external0() interrupt 0
{
if(P17==1)
P17=0;
else
{
P17=0;
}
IE=0x01; // off interrupt
delay(30000);
IE=0x85; // on interrupt
}
void main (void)
{ unsigned int result;
P33 = 1;
IE=0x85;
wrt_command(0x3C); // init LCD
delay(30000);
wrt_command(0x0E); // on LCD and cursor
delay(30000);
set_add(0x42);
show("5221603861");
ADC(&result);
sprintf(name,"%d",result);
set_add(0x00);
show(name);
while (1)
{
}
}
จากคุณ ก้อย (A:192.168.0.82 X:158.108.155.30)
ขอความช่วยเหลือแนะนำ แก้ไข Codeภาษาซี สำหรับไมโครคอนโทรลเลอร์
และไฟ LED กระพริบเมื่อกดคีย์ (เขียนภาษาซีใน Keil V.3)
#include <REG52.H> /* special function register declarations */
#include <ABSACC.H>
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
sbit P33 = P3^3;
sbit P34 = P3^4;
sbit P17 = P1^7;
sbit P30 = P3^0;
sbit P15 = P1^5;
sbit P35 = P3^5;
sbit P31 = P3^1;
sbit P14 = P1^4;
sbit P26 = P2^6;
sbit P27 = P2^7;
unsigned char name[21];// MEMORY REQUEST 21 BYTE
void delay(int time)
{
int i;
for(i=0;i<time;i++)
{
}
}
void lcd_ena (void)
{
P33=1;
delay(1000);
P33=0;
delay(1000);
P33=1;
}
void wrt_command(unsigned char com)
{
P34 = 0;
P0 = com;
lcd_ena();
}
void set_add(char addr)
{
P34 = 0;
P0 = 0x80|addr;
lcd_ena();
}
void wrt_data(unsigned char dat)
{
P34 = 1;
P0 = dat;
lcd_ena();
}
void show (char *strings)
{
int i,sl;
sl=strlen(strings);
for (i=0;i<sl;i++)
{
wrt_data(strings[i]);
delay(1000);
}
}
void iToBinary(char *out,char *bufs)
{ unsigned int tempo,i;
tempo=atoi(bufs);
for(i=15;i>0;i--)
{
if((tempo%2)==1)
out[i]=1;
else out[i]=0;
tempo=tempo/2;
}
out[0]=tempo;
}
void DAC(char *words)
{
int i;
char st[18];
iToBinary(st,words);
P15=0;
// delay(100);
for(i=0;i<16;i++)
{
P31=st[i];
P30=1;
delay(1);
P30=0;
delay(1);
}
P15=1;
P35=0;
delay(1);
P35=1;
}
int ipow(int base,int power)
{
if(power>0)
{
return base*ipow(base,power-1);
}
else
{
return 1;
}
}
void clear_buffer(unsigned char *str,int num )
{
int i;
for(i=0;i<num;i++)
{
str[i]=0;
}
}
void ADC(unsigned int *data10)
{
unsigned int i;
unsigned char temp[13],bb;
*data10=0;
clear_buffer(&temp,12);
P30=0;
P14=0;
delay(10);
P30=1;
delay(100);
P30=0;
P30=1;
P30=0;
P30=1;
P30=0;
for(i=10;i>0;i--)
{
P30=1;
P27=1;
delay(1);
if(P27)
temp[i]=1;
else
temp[i]=0;
P30=0;
delay(2);
}
for(i=10;i>0;i--)
{
/* if(temp[i]==1)
wrt_data(48+1);
else
wrt_data(48); */
*data10=*data10+(temp[i]*ipow(2,i-1));
}
}
void external0() interrupt 0
{
if(P17==1)
P17=0;
else
{
P17=0;
}
IE=0x01; // off interrupt
delay(30000);
IE=0x85; // on interrupt
}
void main (void)
{ unsigned int result;
P33 = 1;
IE=0x85;
wrt_command(0x3C); // init LCD
delay(30000);
wrt_command(0x0E); // on LCD and cursor
delay(30000);
set_add(0x42);
show("5221603861");
ADC(&result);
sprintf(name,"%d",result);
set_add(0x00);
show(name);
while (1)
{
}
}
จากคุณ ก้อย (A:192.168.0.82 X:158.108.155.30)