/*#include<reg52.h> #define uint unsigned int #define uchar unsigned char #define uchar unsigned char sbit K1=P3^4; sbit K2=P3^5; sbit ledr=P1^0; sbit ledg=P1^1; sbit ledb=P1^2; bit LEDDirection=0;//LED控制方向0:渐亮1:渐灭 char pwm=0; char pwmr=0; char scw=0;//中断记数 char tt=0; char n; void dealy(uint z); void Timer0Init(void) { TMOD=0x01; TH0=0xff; TL0=0x47; EX0=1; IT0=0; PX0=1; ET0=1; TR0=1; EA=1; } void main() { Timer0Init(); while(1){ if(K1==0) { dealy (1); if(K1==0) {TR0=1; ledr=0; dealy(5); TR0=0; } } if(K2==0) { dealy (1); if(K2==0) { while(1) { ledr=0; //亮 dealy(100-n*10); ledr=1; //熄 dealy(n*10); } } } } } void Time0Isr(void) interrupt 1 { // pwm=0; TH0=0xff; TL0=0x47; scw++; }*/ #include<reg52.h> #define uchar unsigned char bit LEDDirection=0; sbit P2_0=P1^0; sbit key1=P3^4; sbit key2=P3^5; sbit key3=P3^6; uchar zkb,i,t;// zkb指占空比 uchar pwm; void delay(uchar z) { uchar x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void init() //初始化函数 { TMOD=0X01; TH0=(65536-1000)/256; TL0=(65536-1000)%256; EA=1; ET0=1; TR0=1; } void keyscan() //键盘扫描 { P3=0XFF; if(key1==0) { delay(5); if(key1==0) { while(!key1); if(zkb<9) { zkb++; } } } if(key2==0) { delay(5); if(key2==0) { while(!key2); if(zkb>0) { zkb--; } } } if(key3==0) {TR0=1; delay(5); if(key3==0) {while(!key3); if((zkb<=9)&&(0==LEDDirection)) { zkb++; if(zkb>9) { LEDDirection=1; zkb=9; } } if((zkb>=0 )&&(1==LEDDirection)) { zkb--; if(zkb<0 ) { LEDDirection=0; zkb=0 ; //dealy(3000); } } } //pwm=pwmr; } } void main() //主函数 { zkb=2; init(); while(1) { keyscan(); } } void time0(void) interrupt 1 //中断函数 { TH0=(65536-200)/256; TL0=(65536-200)%256; ++i; if(i>10) { i=0; }; if(i<=zkb) { P2_0=1; } else P2_0=0; } /*void time0(void) interrupt 0 //中断函数 { TH0=(65536-1000)/256; TL0=(65536-1000)%256; ++i; if(i>10) { i=0; }; if(i<=zkb) { P2_0=1; } else P2_0=0; }*/
标签: 调光
上传时间: 2016-07-02
上传用户:184890962
# include < reg52.h > # include < 24C02.h> # define uint unsigned int # define uchar unsigned char sbit rs = P3^5; //定义lcd1602的rs端 sbit lcden = P3^4;//定义lcd1602的lcden端口 sbit s1 = P3^0;//定义功能键s1 sbit s2 = P3^1;//定义功能键s2 sbit s3 = P3^2;//定义功能键s3 sbit beep = P2^3;//定义蜂鸣器 uchar count, s1num; char miao,fen,shi; uchar code table[] = "Designer:X_ZL ";//定义初始上电时液晶默认显示状态 void delay_ms( xms ) //定义延时函数 { uint i,j; for( i = xms ; i > 0 ; i --) for( j = 110 ; j > 0 ; j --); }
上传时间: 2016-07-28
上传用户:游戏好吗
基础程序设计 01 闪烁的LED /* 名称闪烁的LED 说明LED按设定的时间间隔闪烁 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit LED=P1^0; //延时 void DelayMS(uint x) { uchar i; while(x--) { for(i=0;i<120;i++); } } //主程序 void main() { while(1) { LED=~LED; DelayMS(150); } }
标签: 基于8051仿真 基于Proteus仿真 基于8051+Proteus仿真
上传时间: 2016-09-19
上传用户:xinhoujue
基础程序设计 01 闪烁的LED /* 名称闪烁的LED 说明LED按设定的时间间隔闪烁 */ #include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit LED=P1^0; //延时 void DelayMS(uint x) { uchar i; while(x--) { for(i=0;i<120;i++); } } //主程序 void main() { while(1) { LED=~LED; DelayMS(150); } }
标签: 51单片机C语言程序设计 51单片机C语言程序设计Proteus仿真实训
上传时间: 2016-09-19
上传用户:xinhoujue
// 学生管理.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text // Foward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, uint, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, uint, WPARAM, LPARAM); struct person { char name[10]; int ID; int cj_yw; int cj_sx; struct person* next; struct person* pro; }per; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage is only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_MY; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, uint message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Mesage handler for about box. LRESULT CALLBACK About(HWND hDlg, uint message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; }
上传时间: 2016-12-29
上传用户:767483511
// 学生管理.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text // Foward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, uint, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, uint, WPARAM, LPARAM); struct person { char name[10]; int ID; int cj_yw; int cj_sx; struct person* next; struct person* pro; }per; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage is only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCSTR)IDC_MY; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HANDLE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, uint message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hWnd, &rt); DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Mesage handler for about box. LRESULT CALLBACK About(HWND hDlg, uint message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; }
标签: 学生 计算器
上传时间: 2016-12-29
上传用户:767483511
/****************temic*********t5557***********************************/ #include <at892051.h> #include <string.h> #include <intrins.h> #include <stdio.h> #define uchar unsigned char #define uint unsigned int #define ulong unsigned long //STC12C2051AD的SFR定义 sfr WDT_CONTR = 0xe1;//stc2051的看门狗?????? /**********全局常量************/ //写卡的命令 #define write_command0 0//写密码 #define write_command1 1//写配置字 #define write_command2 2//密码写数据 #define write_command3 3//唤醒 #define write_command4 4//停止命令 #define TRUE 1 #define FALSE 0 #define OK 0 #define ERROR 255 //读卡的时间参数us #define ts_min 250//270*11.0592/12=249//取近似的整数 #define ts_max 304//330*11.0592/12=304 #define t1_min 73//90*11.0592/12=83:-10调整 #define t1_max 156//180*11.0592/12=166 #define t2_min 184//210*11.0592/12=194 #define t2_max 267//300*11.0592/12=276 //***********不采用中断处理:采用查询的方法读卡时关所有中断****************/ sbit p_U2270B_Standby = P3^5;//p_U2270B_Standby PIN=13 sbit p_U2270B_CFE = P3^3;//p_U2270B_CFE PIN=6 sbit p_U2270B_OutPut = P3^7;//p_U2270B_OutPut PIN=2 sbit wtd_sck = P1^7;//SPI总线 sbit wtd_si = P1^3; sbit wtd_so = P1^2; sbit iic_data = P1^2;//lcd IIC sbit iic_clk = P1^7; sbit led_light = P1^6;//测试绿灯 sbit led_light1 = P1^5;//测试红灯 sbit led_light_ok = P1^1;//读卡成功标志 sbit fengmingqi = P1^5; /***********全局变量************************************/ uchar data Nkey_a[4] = {0xA0, 0xA1, 0xA2, 0xA3};//初始密码 //uchar idata card_snr[4]; //配置字 uchar data bankdata[28] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7}; //存储卡上用户数据(1-7)7*4=28 uchar data cominceptbuff[6] = {1,2,3,4,5,6};//串口接收数组ram uchar command; //第一个命令 uchar command1;// //uint temp; uchar j,i; uchar myaddr = 8; //uchar ywqz_count,time_count; //ywqz jishu: uchar bdata DATA; sbit BIT0 = DATA^0; sbit BIT1 = DATA^1; sbit BIT2 = DATA^2; sbit BIT3 = DATA^3; sbit BIT4 = DATA^4; sbit BIT5 = DATA^5; sbit BIT6 = DATA^6; sbit BIT7 = DATA^7; uchar bdata DATA1; sbit BIT10 = DATA1^0; sbit BIT11 = DATA1^1; sbit BIT12 = DATA1^2; sbit BIT13 = DATA1^3; sbit BIT14 = DATA1^4; sbit BIT15 = DATA1^5; sbit BIT16 = DATA1^6; sbit BIT17 = DATA1^7; bit i_CurrentLevel;//i_CurrentLevel BIT 00H(Saves current level of OutPut pin of U2270B) bit timer1_end; bit read_ok = 0; //缓存定时值,因用同一个定时器 union HLint { uint W; struct { uchar H;uchar L; } B; };//union HLint idata a union HLint data a; //缓存定时值,因用同一个定时器 union HLint0 { uint W; struct { uchar H; uchar L; } B; };//union HLint idata a union HLint0 data b; /**********************函数原型*****************/ //读写操作 void f_readcard(void);//全部读出1~7 AOR唤醒 void f_writecard(uchar x);//根据命令写不同的内容和操作 void f_clearpassword(void);//清除密码 void f_changepassword(void);//修改密码 //功能子函数 void write_password(uchar data *data p);//写初始密码或数据 void write_block(uchar x,uchar data *data p);//不能用通用指针 void write_bit(bit x);//写位 /*子函数区*****************************************************/ void delay_2(uint x) //延时,时间x*10us@12mhz,最小20us@12mhz { x--; x--; while(x) { _nop_(); _nop_(); x--; } _nop_();//WDT_CONTR=0X3C;不能频繁的复位 _nop_(); } ///////////////////////////////////////////////////////////////////// void initial(void) { SCON = 0x50; //串口方式1,允许接收 //SCON =0x50; //01010000B:10位异步收发,波特率可变,SM2=0不用接收到有效停止位才RI=1, //REN=1允许接收 TMOD = 0x21; //定时器1 定时方式2(8位),定时器0 定时方式1(16位) TCON = 0x40; //设定时器1 允许开始计时(IT1=1) TH1 = 0xfD; //FB 18.432MHz 9600 波特率 TL1 = 0xfD; //fd 11.0592 9600 IE = 0X90; //EA=ES=1 TR1 = 1; //启动定时器 WDT_CONTR = 0x3c;//使能看门狗 p_U2270B_Standby = 0;//单电源 PCON = 0x00; IP = 0x10;//uart you xian XXXPS PT1 PX1 PT0 PX0 led_light1 = 1; led_light = 0; p_U2270B_OutPut = 1; } /************************************************/ void f_readcard()//读卡 { EA = 0;//全关,防止影响跳变的定时器计时 WDT_CONTR = 0X3C;//喂狗 p_U2270B_CFE = 1;// delay_2(232); //>2.5ms /* // aor 用唤醒功能来防碰撞 p_U2270B_CFE = 0; delay_2(18);//start gap>150us write_bit(1);//10=操作码读0页 write_bit(0); write_password(&bankdata[24]);//密码block7 p_U2270B_CFE =1 ;// delay_2(516);//编程及确认时间5.6ms */ WDT_CONTR = 0X3C;//喂狗 led_light = 0; b.W = 0; while(!(read_ok == 1)) { //while(p_U2270B_OutPut);//等一个稳定的低电平?超时判断? while(!p_U2270B_OutPut);//等待上升沿的到来同步信号检测1 TR0 = 1; //deng xia jiang while(p_U2270B_OutPut);//等待下降沿 TR0 = 0; a.B.H = TH0; a.B.L = TL0; TH0 = TL0 = 0; TR0 = 1;//定时器晚启动10个周期 //同步头 if((324 < a.W) && (a.W < 353)) ;//检测同步信号1 else { TR0 = 0; TH0 = TL0 = 0; goto read_error; } //等待上升沿 while(!p_U2270B_OutPut); TR0 = 0; a.B.H = TH0; a.B.L = TL0; TH0 = TL0 = 0; TR0 = 1;//b.N1<<=8; if(a.B.L < 195);//0.5p else { TR0 = 0; TH0 = TL0 = 0; goto read_error; } //读0~7块的数据 for(j = 0;j < 28;j++) { //uchar i; for(i = 0;i < 16;i++)//8个位 { //等待下降沿的到来 while(p_U2270B_OutPut); TR0 = 0; a.B.H = TH0; a.B.L = TL0; TH0 = TL0 = 0; TR0 = 1; if(t2_max < a.W/*)&&(a.W < t2_max)*/)//1P { b.W >>= 2;//先左移再赋值 b.B.L += 0xc0; i++; } else if(t1_min < a.B.L/*)&&(a.B.L < t1_max)*/)//0.5p { b.W >>= 1; b.B.L += 0x80; } else { TR0 = 0; TH0 = TL0 = 0; goto read_error; } i++; while(!p_U2270B_OutPut);//上升 TR0 = 0; a.B.H = TH0; a.B.L = TL0; TH0 = TL0 = 0; TR0 = 1; if(t2_min < a.W/*)&&(a.W < t2_max)*/)//1P { b.W >>= 2; i++; } else if(t1_min < a.B.L/*a.W)&&(a.B.L < t1_max)*/)//0.5P //else if(!(a.W==0)) { b.W >>= 1; //temp+=0x00; //led_light1=0;led_light=1;delay_2(40000); } else { TR0 = 0; TH0 = TL0 = 0; goto read_error; } i++; } //取出奇位 DATA = b.B.L; BIT13 = BIT7; BIT12 = BIT5; BIT11 = BIT3; BIT10 = BIT1; DATA = b.B.H; BIT17 = BIT7; BIT16 = BIT5; BIT15 = BIT3; BIT14 = BIT1; bankdata[j] = DATA1; } read_ok = 1;//读卡完成了 read_error: _nop_(); } } /***************************************************/ void f_writecard(uchar x)//写卡 { p_U2270B_CFE = 1; delay_2(232); //>2.5ms //psw=0 standard write if (x == write_command0)//写密码:初始化密码 { uchar i; uchar data *data p; p = cominceptbuff; p_U2270B_CFE = 0; delay_2(31);//start gap>330us write_bit(1);//写操作码1:10 write_bit(0);//写操作码0 write_bit(0);//写锁定位0 for(i = 0;i < 35;i++) { write_bit(1);//写数据位1 } p_U2270B_CFE = 1; led_light1 = 0; led_light = 1; delay_2(40000);//测试使用 //write_block(cominceptbuff[4],p); p_U2270B_CFE = 1; bankdata[20] = cominceptbuff[0];//密码存入 bankdata[21] = cominceptbuff[1]; bankdata[22] = cominceptbuff[2]; bankdata[23] = cominceptbuff[3]; } else if (x == write_command1)//配置卡参数:初始化 { uchar data *data p; p = cominceptbuff; write_bit(1);//写操作码1:10 write_bit(0);//写操作码0 write_bit(0);//写锁定位0 write_block(cominceptbuff[4],p); p_U2270B_CFE= 1; } //psw=1 pssword mode else if(x == write_command2) //密码写数据 { uchar data*data p; p = &bankdata[24]; write_bit(1);//写操作码1:10 write_bit(0);//写操作码0 write_password(p);//发口令 write_bit(0);//写锁定位0 p = cominceptbuff; write_block(cominceptbuff[4],p);//写数据 } else if(x == write_command3)//aor //唤醒 { //cominceptbuff[1]操作码10 X xxxxxB uchar data *data p; p = cominceptbuff; write_bit(1);//10 write_bit(0); write_password(p);//密码 p_U2270B_CFE = 1;//此时数据不停的循环传出 } else //停止操作码 { write_bit(1);//11 write_bit(1); p_U2270B_CFE = 1; } p_U2270B_CFE = 1; delay_2(560);//5.6ms } /************************************/ void f_clearpassword()//清除密码 { uchar data *data p; uchar i,x; p = &bankdata[24];//原密码 p_U2270B_CFE = 0; delay_2(18);//start gap>150us //操作码10:10xxxxxxB write_bit(1); write_bit(0); for(x = 0;x < 4;x++)//发原密码 { DATA = *(p++); for(i = 0;i < 8;i++) { write_bit(BIT0); DATA >>= 1; } } write_bit(0);//锁定位0:0 p = &cominceptbuff[0]; write_block(0x00,p);//写新配置参数:pwd=0 //密码无效:即清除密码 DATA = 0x00;//停止操作码00000000B for(i = 0;i < 2;i++) { write_bit(BIT7); DATA <<= 1; } p_U2270B_CFE = 1; delay_2(560);//5.6ms } /*********************************/ void f_changepassword()//修改密码 { uchar data *data p; uchar i,x,addr; addr = 0x07;//block7 p = &Nkey_a[0];//原密码 DATA = 0x80;//操作码10:10xxxxxxB for(i = 0;i < 2;i++) { write_bit(BIT7); DATA <<= 1; } for(x = 0;x < 4;x++)//发原密码 { DATA = *(p++); for(i = 0;i < 8;i++) { write_bit(BIT7); DATA >>= 1; } } write_bit(0);//锁定位0:0 p = &cominceptbuff[0]; write_block(0x07,p);//写新密码 p_U2270B_CFE = 1; bankdata[24] = cominceptbuff[0];//密码存入 bankdata[25] = cominceptbuff[1]; bankdata[26] = cominceptbuff[2]; bankdata[27] = cominceptbuff[3]; DATA = 0x00;//停止操作码00000000B for(i = 0;i < 2;i++) { write_bit(BIT7); DATA <<= 1; } p_U2270B_CFE = 1; delay_2(560);//5.6ms } /***************************子函数***********************************/ void write_bit(bit x)//写一位 { if(x) { p_U2270B_CFE = 1; delay_2(32);//448*11.0592/120=42延时448us p_U2270B_CFE = 0; delay_2(28);//280*11.0592/120=26写1 } else { p_U2270B_CFE = 1; delay_2(92);//192*11.0592/120=18 p_U2270B_CFE = 0; delay_2(28);//280*11.0592/120=26写0 } } /*******************写一个block*******************/ void write_block(uchar addr,uchar data *data p) { uchar i,j; for(i = 0;i < 4;i++)//block0数据 { DATA = *(p++); for(j = 0;j < 8;j++) { write_bit(BIT0); DATA >>= 1; } } DATA = addr <<= 5;//0地址 for(i = 0;i < 3;i++) { write_bit(BIT7); DATA <<= 1; } } /*************************************************/ void write_password(uchar data *data p) { uchar i,j; for(i = 0;i < 4;i++)// { DATA = *(p++); for(j = 0;j < 8;j++) { write_bit(BIT0); DATA >>= 1; } } } /*************************************************/ void main() { initial(); TI = RI = 0; ES = 1; EA = 1; delay_2(28); //f_readcard(); while(1) { f_readcard(); //读卡 f_writecard(command1); //写卡 f_clearpassword(); //清除密码 f_changepassword(); //修改密码 } }
标签: 12345
上传时间: 2017-10-20
上传用户:my_lcs
在本课中,我们要用一个按键来实现跑马灯的 10 级调速。这又会涉及到键的去抖的问 题。 本课的试验结果是,每按一次按键,跑马速度就降低一级,共 10 级。 这里我们又增加了一个变量 speedlever,来保存当前的速度档次。 在按键里的处理中,多了当前档次的延时值的设置。 请看程序: ―――――――――――――――― #define uchar unsigned char //定义一下方便使用 #define uint unsigned int #define ulong unsigned long #include <reg52.h> //包括一个 52 标准内核的头文件 sbit P10 = P1^0; //头文件中没有定义的 IO 就要自己来定义了 sbit P11 = P1^1; sbit P12 = P1^2; sbit P13 = P1^3; sbit K1= P3^2; bit ldelay=0; //长定时溢出标记,预置是 0 uchar speed=10; //设置一个变量保存默认的跑马灯的移动速度 uchar speedlever=0; //保存当前的速度档次 char code dx516[3] _at_ 0x003b;//这是为了仿真设置的 //一个按键控制的 10 级变速跑马灯试验 void main(void) // 主程序 { uchar code ledp[4]={0xfe,0xfd,0xfb,0xf7};//预定的写入 P1 的值 uchar ledi; //用来指示显示顺序 uint n; RCAP2H =0x10; //赋 T2 的预置值 0x1000,溢出 30 次就是 1 秒钟 RCAP2L =0x00; TR2=1; //启动定时器 ET2=1; //打开定时器 2 中断 EA=1; //打开总中断 while(1) //主程序循环 { if(ldelay) //发现有时间溢出标记,进入处理 { ldelay=0; //清除标记 P1=ledp[ledi]; //读出一个值送到 P1 口 ledi++; //指向下一个 if(ledi==4) { ledi=0; //到了最后一个灯就换到第一个 } } if(!K1) //如果读到 K1 为 0 { for(n=0;n<1000;n++); //等待按键稳定 while(!K1); //等待按键松开 for(n=0;n<1000;n++); //等待按键稳定松开 speedlever++; if(speedlever==10)speedlever=0; speed=speedlever*3; //档次和延时之间的预算法则,也可以用查表方法,做出 不规则的法则 } } } //定时器 2 中断 timer2() interrupt 5 { static uchar t; TF2=0; t++; if((t==speed)||(t>30)) //比较一个变化的数值,以实现变化的时间溢出,同时限制了最慢速 度为 1 秒 { t=0; ldelay=1;//每次长时间的溢出,就置一个标记,以便主程序处理 } } ―――――――――――――――――――――― 请打开 lesson11 目录的工程,编译,运行,看结果: 按 K1,速度则降低一次,总共 10 个档次。
上传时间: 2017-11-06
上传用户:szcyclone
LED 一般是恒流操作的,如何改变 LED 的亮度呢?答案就是 PWM 控制。在一定的 频率的方波中,调整高电平和低电平的占空比,即可实现。比如我们用低电平点亮一个 LED 灯,我们假设把一个频率周期分为 10 个时间等份,如果方波中的高低电平占空比是 9:1, 这是就是一个比较暗的亮度,如果方波中高低电平占空比是 10:0,这时,全部是高电平, 灯是灭的。如果占空比是 5:5,就是一个中间亮度,如果高低比是 1:9,是一个比较亮的 亮度,如果高低是 0:10,这时全部是低电平,就是最亮的。 实际上应用中,电视屏幕墙中的几十百万 LED 象素都是这样控制的,而且每一个象素 都有红绿蓝 3 个 LED,每个 LED 可以变化的亮度是几百到几万或者更多的级别,以实现真 彩色的显示。还有在您的手机中,背光灯的亮度如果是可以变化的,也应该是这种工作方式。 目前的城市彩灯也有很多都使用了 LED,需要控制亮度是也是 PWM 控制。 下面来分析我们的例程,在这个例程中,我们将定时器 2 溢出定为 1/1200 秒。每 10 次脉冲输出一个 120HZ 频率。这每 10 次脉冲再用来控制高低电平的 10 个比值。这样,在 每个 1/120 秒的方波周期中,我们都可以改变方波的输出占空比,从而控制 LED 灯的 10 个 级别的亮度。 为什么输出方波的频率要 120HZ 这么高?因为如果频率太低,人眼就会看到闪烁感 觉。一般起码要在 60HZ 以上才感觉好点,120HZ 就基本上看不到闪烁,只能看到亮度的变 化了。 下面请看程序,程序中有比较多的注释: ――――――――――――――――――――――― #define uchar unsigned char //定义一下方便使用 #define uint unsigned int #define ulong unsigned long #include <reg52.h> //包括一个 52 标准内核的头文件 sbit P10 = P1^0; //要控制的 LED 灯 sbit K1= P3^2; //按键 K1 uchar scale;//用于保存占空比的输出 0 的时间份额,总共 10 份 char code dx516[3] _at_ 0x003b;//这是为了仿真设置的 //模拟 PWM 输出控制灯的 10 个亮度级别 void main(void) // 主程序 { uint n; RCAP2H =0xF3; //赋 T2 的预置值,溢出 1 次是 1/1200 秒钟 RCAP2L =0x98; TR2=1; //启动定时器 ET2=1; //打开定时器 2 中断 EA=1; //打开总中断 while(1) //程序循环 { ;//主程序在这里就不断自循环,实际应用中,这里是做主要工作 for(n=0;n<50000;n++); //每过一会儿就自动加一个档次的亮度 scale++; if(scale==10)scale=0; } } //1/1200 秒定时器 2 中断 timer2() interrupt 5 { static uchar tt; //tt 用来保存当前时间在一秒中的比例位置 TF2=0; tt++; if(tt==10) //每 1/120 秒整开始输出低电平 { tt=0; if(scale!=0) //这里加这一句是为了消除灭灯状态产生的鬼影 P10=0; } if(scale==tt) //按照当前占空比切换输出高电平 P10=1; } ―――――――――――――――――― 在主程序中,每延时一段时间,就自动换一个占空比,以使亮度自动变化,方便观察。 编译,运行,看结果。 可以看到,LED 的亮度以每种亮度 1 秒左右不断变化,共有 10 个级别。
上传时间: 2017-11-06
上传用户:szcyclone
include<reg52.h> #define uint unsigned int #define uchar unsigned char uint temp,aa,wang,qian,bai,shi,ge; sbit dula=P2^6; sbit wela=P2^7; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void display( uint wang,uint qian,uint bai,uint shi,uint ge); void delay(uint z); void init(); void main() { init();//初始化子程序 while(1) { if(aa==20) { aa=0; temp++; if(temp==99999) { temp=0; } wang=temp/10000; qian=(temp-wang*10000)/1000; bai=(temp-wang*10000-qian*1000)/100; shi=(temp-wang*10000-qian*1000-bai*100)/10; ge=temp%10; } display(wang,qian, bai,shi,ge); } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uint wang,uint qian,uint bai,uint shi,uint ge) { dula=1; P0=table[wang]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=table[qian]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=table[bai]; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=table[shi]; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); dula=1; P0=table[ge]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(1); } void init() { wela=0; dula=0; temp=0; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; aa++; } include<reg52.h> #define uint unsigned int #define uchar unsigned char uint temp,aa,wang,qian,bai,shi,ge; sbit dula=P2^6; sbit wela=P2^7; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void display( uint wang,uint qian,uint bai,uint shi,uint ge); void delay(uint z); void init(); void main() { init();//初始化子程序 while(1) { if(aa==20) { aa=0; temp++; if(temp==99999) { temp=0; } wang=temp/10000; qian=(temp-wang*10000)/1000; bai=(temp-wang*10000-qian*1000)/100; shi=(temp-wang*10000-qian*1000-bai*100)/10; ge=temp%10; } display(wang,qian, bai,shi,ge); } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uint wang,uint qian,uint bai,uint shi,uint ge) { dula=1; P0=table[wang]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=table[qian]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=table[bai]; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=table[shi]; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); dula=1; P0=table[ge]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(1); } void init() { wela=0; dula=0; temp=0; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; aa++; } include<reg52.h> #define uint unsigned int #define uchar unsigned char uint temp,aa,wang,qian,bai,shi,ge; sbit dula=P2^6; sbit wela=P2^7; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void display( uint wang,uint qian,uint bai,uint shi,uint ge); void delay(uint z); void init(); void main() { init();//初始化子程序 while(1) { if(aa==20) { aa=0; temp++; if(temp==99999) { temp=0; } wang=temp/10000; qian=(temp-wang*10000)/1000; bai=(temp-wang*10000-qian*1000)/100; shi=(temp-wang*10000-qian*1000-bai*100)/10; ge=temp%10; } display(wang,qian, bai,shi,ge); } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uint wang,uint qian,uint bai,uint shi,uint ge) { dula=1; P0=table[wang]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(1); dula=1; P0=table[qian]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(1); dula=1; P0=table[bai]; dula=0; P0=0xff; wela=1; P0=0xfb; wela=0; delay(1); dula=1; P0=table[shi]; dula=0; P0=0xff; wela=1; P0=0xf7; wela=0; delay(1); dula=1; P0=table[ge]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(1); } void init() { wela=0; dula=0; temp=0; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; aa++; }
标签: 矩阵式键盘
上传时间: 2021-12-18
上传用户:2590813506