前几天,发现了这个51hei网站,突发奇想,向大家推荐了一个51编译器,虽然得到了黑班主的50黑币,但是没有任何51黑弟兄们的捧场,实在遗憾。
今后,我会经常到这里来溜达溜达,推荐一些常见电路简单编程(诸如 LCD 16x2 or 16x4, LCD 12864, I2C, SPI, SD Card, 等等........) 。希望大家喜欢。
以下是一个简单的 LCD 16x2 的单片机小程序:
- // Lcd module connections
- sbit LCD_RS at P2_0_bit;
- sbit LCD_EN at P2_1_bit;
- sbit LCD_D4 at P2_2_bit;
- sbit LCD_D5 at P2_3_bit;
- sbit LCD_D6 at P2_4_bit;
- sbit LCD_D7 at P2_5_bit;
- // End Lcd module connections
- char txt1[] = "mikroElektronika";
- char txt2[] = "Easy8051B";
- char txt3[] = "Lcd4bit";
- char txt4[] = "example";
- char i; // Loop variable
- void Move_Delay() { // Function used for text moving
- Delay_ms(500); // You can change the moving speed here
- }
- void main(){
- Lcd_Init(); // Initialize Lcd
- Lcd_Cmd(_LCD_CLEAR); // Clear display
- Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
- Lcd_Out(1,6,txt3); // Write text in first row
- Lcd_Out(2,6,txt4); // Write text in second row
- Delay_ms(2000);
- Lcd_Cmd(_LCD_CLEAR); // Clear display
- Lcd_Out(1,1,txt1); // Write text in first row
- Lcd_Out(2,5,txt2); // Write text in second row
- Delay_ms(2000);
- // Moving text
- for(i=0; i<4; i++) { // Move text to the right 4 times
- Lcd_Cmd(_LCD_SHIFT_RIGHT);
- Move_Delay();
- }
- while(1) { // Endless loop
- for(i=0; i<8; i++) { // Move text to the left 7 times
- Lcd_Cmd(_LCD_SHIFT_LEFT);
- Move_Delay();
- }
- for(i=0; i<8; i++) { // Move text to the right 7 times
- Lcd_Cmd(_LCD_SHIFT_RIGHT);
- Move_Delay();
- }
- }
- }
复制代码 附:简单电路图。
|