【问题标题】:MPLAB code not workingMPLAB 代码不工作
【发布时间】:2014-08-19 13:19:39
【问题描述】:

我正在从事 pic 微控制器编程,最近去 lcd 制作了 2 个代码,一个在 mplab 中,另一个在 mikroc 实际上都在 isis 中工作,但是当我真正尝试它时,只有 mikroc 代码在微控制器中工作我不实际上知道是什么问题/为什么会发生这种情况我不认为这是硬件或软件代码,因为它是相同的电路并且两个代码都在 isis 中工作所以如果有人想查看代码,那么这里是:

mikroC:

    // Lcd pinout settings
    sbit LCD_RS at RC0_bit;
    sbit LCD_EN at RC1_bit;
    sbit LCD_D7 at RB7_bit;
    sbit LCD_D6 at RB6_bit;
    sbit LCD_D5 at RB5_bit;
    sbit LCD_D4 at RB4_bit;

    // Pin direction
    sbit LCD_RS_Direction at TRISC0_bit;
    sbit LCD_EN_Direction at TRISC1_bit;
    sbit LCD_D7_Direction at TRISB7_bit;
    sbit LCD_D6_Direction at TRISB6_bit;
    sbit LCD_D5_Direction at TRISB5_bit;
    sbit LCD_D4_Direction at TRISB4_bit;

    void main() {
         Lcd_Init();
         Lcd_Cmd(_LCD_CLEAR);               // Clear display
         Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

         Lcd_Out(1, 3, "Hello!");

    }

mplab:

main.c:

       #include "config.h"

        unsigned char CurvyObject[8] = { 0x01,0x02,0x04,0x08,0x10,0x11,0x1f,0x00 };

        void main(){
            TRISB = 0;
            TRISC = 0;
            send_command(0x38);
            __delay_us(40);
            send_command(0x01);
            __delay_ms(1.75);
            send_command(0x0C);
            __delay_us(40);
            moveto(0, 0, 0, 0, 0, 0, 0);
            __delay_us(40);
            write_character(70);
            __delay_us(50);
            write_character(97);
            __delay_us(50);
            write_character(100);
            __delay_us(50);
            write_character(121);
            __delay_us(50);

            while(1);


        }

config.h:

/* 
             * File:   config.h
             * Author: Fady
             *
             * Created on August 14, 2014, 6:19 PM
             */


            // PIC16F877A Configuration Bit Settings

            // 'C' source line config statements

            #include <xc.h>

            // #pragma config statements should precede project file includes.
            // Use project enums instead of #define for ON and OFF.

            // CONFIG
            #pragma config FOSC = XT        
            #pragma config WDTE = OFF       
            #pragma config PWRTE = OFF      
            #pragma config BOREN = ON       
            #pragma config LVP = ON         
            #pragma config CPD = OFF        
            #pragma config WRT = OFF        
            #pragma config CP = OFF         

            #define _XTAL_FREQ 4000000

            unsigned char i;

            //Library Declaration:
            void send_command(int command);
            void write_character(int character);
            void enable_blink();
            void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);

            void send_command(int command){
                PORTB = command;
                PORTCbits.RC0 = 0;
                enable_blink();

            }

            void write_character(int character){
                PORTB = character;
                PORTCbits.RC0 = 1;
                enable_blink();

            }

            void enable_blink(){
                PORTCbits.RC1 = 1;
                __delay_ms(10);
                PORTCbits.RC1 = 0;
                __delay_ms(10);

            }

            void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
                PORTCbits.RC0 = 0;
                PORTBbits.RB7 = 1;

                PORTBbits.RB6 = b6;
                PORTBbits.RB5 = b5;
                PORTBbits.RB4 = b4;
                PORTBbits.RB3 = b3;
                PORTBbits.RB2 = b2;
                PORTBbits.RB1 = b1;
                PORTBbits.RB0 = b0;

                enable_blink();

            }

【问题讨论】:

  • 在将“命令”设置到 PORTB 之前,请确保正确设置了 RS 和 RW 线。您在将值放入 PORT 值之后执行此操作,因此它无法识别它是命令、读取还是其他任何内容。我从您的另一篇文章中看到您已经添加了我所说的 enableBLing 功能。如果您可以使用逻辑分析仪或示波器,请发布它提供给您的信号图片。

标签: pic microchip mplab mikroc


【解决方案1】:

在启动程序之前确保所有外围设备和输出都处于已知状态是一种很好的做法。 正如评论中提到的,您从未确保 LCD 的 R/W 引脚配置正确。

PORTCbits.RC2=0; // The Microcontroller will always write to the LCD   
                 // Replace RC2 with your R/W pin

除此之外,我将您的 LCD 库与我制作的 LCD 库进行了比较,并且您没有配置进入模式,请尝试添加以下内容:

send_command(6);      //Cursor Move direction and Display Shift

查看此通用数据表,了解您是否按照需要配置了模块:http://www.lcd-module.com/eng/pdf/doma/dip162-de.pdf

在旁注中,我看到 moveto();函数用于定位光标,我可以建议我的方法:

// L1=128,L2=192,L3=144,L4=208 
send_command(L1+6);    // Go to the 6th character of the first line

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多