在C中为联盟中的另一个变量分配变量

时间:2018-01-03 12:38:46

标签: c variables structure unions mplab

我正在使用MPLAB X IDE 3.60开发一个项目。其中有一些联合/结构定义。它们用于驱动7段LCD。

我无法使用其名称将变量传递给另一个变量。它会分配一个随机值。但是,我可以将值本身分配给相关变量,LCD工作正常。

的main.c

#define _XTAL_FREQ 40000000
#if defined(__XC)
    #include <xc.h>        /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>       /* HiTech General Include File */
#elif defined(__18CXX)
    #include <p18cxxx.h>   /* C18 General Include File */
#endif

#if defined(__XC) || defined(HI_TECH_C)

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#endif

#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */
#include "lcd_driver.h"

//CONFIG
#pragma config SOSCSEL = DIG
#pragma config FOSC = HS1       // Oscillator (HS oscillator (Medium power, 4 MHz - 16 MHz))
#pragma config PLLCFG = ON      // PLL x4 Enable bit (Enabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)



void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
    //Variables 

    ANCON2 = 0x00;
    CCP6OD = 0; // RG3, comparator disabled.
    OSCCONbits.SCS = 0;


    LcdInit();
    LCD_7SEG();
    LCD_Update();

    while(1)
    {

    } 
}

lcd_driver.c

#include <xc.h>

#include "lcd_driver.h"
#include "defines.h"
#include <string.h>
#include <stdio.h>


volatile StructLCD      MediumLCDBuffer; // Not used yet
volatile StructLCD      MediumLCD @0xF66;

volatile UDigit UpdatedDigit;


void LcdInit(void)
{
    // Init ports function for LCD driver
    LCDSE0bits.SE00 = 1;
    LCDSE0bits.SE01 = 1;
    LCDSE0bits.SE02 = 1;
    LCDSE0bits.SE03 = 1;
    LCDSE0bits.SE04 = 1;
    LCDSE0bits.SE05 = 1;
    LCDSE0bits.SE06 = 1;
    LCDSE0bits.SE07 = 1;

    LCDSE1bits.SE08 = 1;
    LCDSE1bits.SE09 = 1;
    LCDSE1bits.SE10 = 1;
    LCDSE1bits.SE11 = 1;
    LCDSE1bits.SE12 = 1;
    LCDSE1bits.SE13 = 1;
    LCDSE1bits.SE14 = 1;
    LCDSE1bits.SE15 = 1;

    LCDSE2bits.SE18 = 1;
    LCDSE2bits.SE19 = 1;
    LCDSE2bits.SE20 = 1;
    LCDSE2bits.SE21 = 1;
    LCDSE2bits.SE22 = 1;
    LCDSE2bits.SE23 = 1;

    LCDSE3bits.SE24 = 1;
    LCDSE3bits.SE25 = 1;
    LCDSE3bits.SE26 = 1; 
    LCDSE3bits.SE29 = 1;
    LCDSE3bits.SE29 = 1;
    LCDSE3bits.SE30 = 1;

    // Reference ladder interval control
    LCDRLbits.LRLAT0 = 1;
    LCDRLbits.LRLAT1 = 1;
    LCDRLbits.LRLAT2 = 1;

    // Reference ladder A Power Control
    LCDRLbits.LRLAP0 = 1;
    LCDRLbits.LRLAP1 = 1;

    // Reference ladder B Power Control
    LCDRLbits.LRLBP0 = 1;
    LCDRLbits.LRLBP1 = 1;

    // Select internal bias
    LCDREFbits.VLCD1PE = 0;
    LCDREFbits.VLCD2PE = 0;
    LCDREFbits.VLCD3PE = 0;

    // Enable internal reference
    LCDREFbits.LCDIRE = 1;

    // Select internal reference source (VDD)
    LCDREFbits.LCDIRS = 0;

    // Select LCD contrast (0 = Max contrast, 7 = min contrast)
    LCDREFbits.LCDCST0 = 1;
    LCDREFbits.LCDCST1 = 0;
    LCDREFbits.LCDCST2 = 0;

    /* LCDCON register */
    LCDCONbits.LCDEN = 1; //Enable LCD Module
    LCDCONbits.SLPEN = 0;
    LCDCONbits.WERR = 0;
    LCDCONbits.CS = 0; // (Fosc/4)/8192))
    LCDCONbits.LMUX = 3; // <1:0> 1/4 COM, 1/3 BIAS 


    /* LCDPS register  */
    LCDPSbits.WFT = 0; // Type-A Waveform Selection.
    LCDPSbits.BIASMD = 0;
    LCDPSbits.LCDA = 1;
    LCDPSbits.WA = 0;
    // LCD prescaler bits
    LCDPSbits.LP0 = 1;
    LCDPSbits.LP1 = 1;
    LCDPSbits.LP2 = 0; // 1:4
    LCDPSbits.LP3 = 0;
}

void LCD_7SEG(void)
{         
    unsigned char buffer = 0x12; // assign a value
    //printf("%u", (unsigned char) buffer); // Does not work without this line, It assigns random value instead of buffer value
    UpdatedDigit.DigitChar = buffer;        
}

void LCD_Update(void)
{        
    MediumLCD.Seg10A = UpdatedDigit.DigitBit.SEG_A;   MediumLCD.Seg10B = UpdatedDigit.DigitBit.SEG_B;
    MediumLCD.Seg10C = UpdatedDigit.DigitBit.SEG_C;   MediumLCD.Seg10D = UpdatedDigit.DigitBit.SEG_D;
    MediumLCD.Seg10E = UpdatedDigit.DigitBit.SEG_E;   MediumLCD.Seg10F = UpdatedDigit.DigitBit.SEG_F;
    MediumLCD.Seg10G = UpdatedDigit.DigitBit.SEG_G;

}

lcd_driver.h

#ifndef LCD_DRIVER_H
#define LCD_DRIVER_H

#ifdef  __cplusplus
extern "C" {
#endif

#ifdef  __cplusplus
}
#endif  


typedef struct 
{
    // -- COM 3 (LCD)-- //)         PIC(No) |   LCD(no))
    unsigned char ECOLOWER      :1; //Seg0      LCD1
    unsigned char Seg5D         :1; //Seg1      LCD2
    unsigned char DP5           :1; //Seg2      LCD3
    unsigned char Seg6D         :1; //Seg3      LCD4
    unsigned char DP6           :1; //Seg4      LCD5
    unsigned char Seg7D         :1; //Seg5      LCD6
    unsigned char DEGLOWER      :1; //Seg6      LCD7
    unsigned char ECOUPPER      :1; //Seg7      LCD8    (LCD9 -> SEG30)

    unsigned char DP7           :1; //Seg8      LCD10
    unsigned char Seg9D         :1; //Seg9      LCD11
    unsigned char DP8           :1; //Seg10     LCD12
    unsigned char Seg10D        :1; //Seg11     LCD13   (LCD14 -> SEG29)
    unsigned char kW            :1; //Seg12     LCD15
    unsigned char BarFrame_09   :1; //Seg13     LCD16
    unsigned char BarGraph_I5   :1; //Seg14     LCD17
    unsigned char BarFrame_I5   :1; //Seg15     LCD18    

    unsigned char Dummy1        :1; //Seg16     Disabled!
    unsigned char Dummy2        :1; //Seg17     Disabled!
    unsigned char Seg4B         :1; //Seg18     LCD19
    unsigned char Seg4A         :1; //Seg19     LCD20
    unsigned char Seg3B         :1; //Seg20     LCD21
    unsigned char Seg3A         :1; //Seg21     LCD22
    unsigned char Seg2B         :1; //Seg22     LCD23
    unsigned char Seg2A         :1; //Seg23     LCD24

    unsigned char Seg1B         :1; //Seg24     LCD25
    unsigned char Seg1A         :1; //Seg25     LCD26
    unsigned char Service       :1; //Seg26     LCD27
    unsigned char Dummy3        :1; //Seg27     Disabled
    unsigned char Dummy4        :1; //Seg28     Disabled
    unsigned char DummyNC       :1; //Seg29     LCD14
    unsigned char Seg8D         :1; //Seg30     LCD9
    unsigned char Dummy5        :1; //Seg31     Disabled

    unsigned char Dummy6        :8; //LCDDATA4 not available
    unsigned char Dummy7        :8; //LCDDATA5 not available

    // -- COM 2 (LCD)-- //)         PIC(No) |   LCD(no))
    unsigned char COD           :1; //Seg0      LCD1
    unsigned char Seg5E         :1; //Seg1      LCD2
    unsigned char Seg5C         :1; //Seg2      LCD3
    unsigned char Seg6E         :1; //Seg3      LCD4
    unsigned char Seg6C         :1; //Seg4      LCD5
    unsigned char Seg7E         :1; //Seg5      LCD6
    unsigned char Seg7C         :1; //Seg6      LCD7
    unsigned char VAL           :1; //Seg7      LCD8    (LCD9 -> SEG30)

    unsigned char Seg8C         :1; //Seg8      LCD10
    unsigned char Seg9E         :1; //Seg9      LCD11
    unsigned char Seg9C         :1; //Seg10     LCD12
    unsigned char Seg10E        :1; //Seg11     LCD13   (LCD14 -> SEG29)
    unsigned char Lmin          :1; //Seg12     LCD15
    unsigned char BarFrame_I9   :1; //Seg13     LCD16
    unsigned char BarFrame_I6   :1; //Seg14     LCD17
    unsigned char BarGraph_I6   :1; //Seg15     LCD18    

    unsigned char Dummy8        :1; //Seg16     Disabled!
    unsigned char Dummy9        :1; //Seg17     Disabled!
    unsigned char Seg4G         :1; //Seg18     LCD19
    unsigned char Seg4F         :1; //Seg19     LCD20
    unsigned char Seg3G         :1; //Seg20     LCD21
    unsigned char Seg3F         :1; //Seg21     LCD22
    unsigned char Seg2G         :1; //Seg22     LCD23
    unsigned char Seg2F         :1; //Seg23     LCD24

    unsigned char Seg1G         :1; //Seg24     LCD25
    unsigned char Seg1F         :1; //Seg25     LCD26
    unsigned char WaterDrop     :1; //Seg26     LCD27
    unsigned char Dummy10       :1; //Seg27     Disabled
    unsigned char Dummy11       :1; //Seg28     Disabled
    unsigned char Seg10C        :1; //Seg29     LCD14
    unsigned char Seg8E         :1; //Seg30     LCD9
    unsigned char Dummy12       :1; //Seg31     Disabled

    unsigned char Dummy13       :8; //LCDDATA10 not available
    unsigned char Dummy14       :8; //LCDDATA11 not available

    // -- COM 1 (LCD)-- //)         PIC(No) |   LCD(no)
    unsigned char TAP           :1; //Seg0      LCD1
    unsigned char Seg5F         :1; //Seg1      LCD2
    unsigned char Seg5G         :1; //Seg2      LCD3
    unsigned char Seg6F         :1; //Seg3      LCD4
    unsigned char Seg6G         :1; //Seg4      LCD5
    unsigned char Seg7F         :1; //Seg5      LCD6
    unsigned char Seg7G         :1; //Seg6      LCD7
    unsigned char Radiator      :1; //Seg7      LCD8    (LCD9 -> SEG30)

    unsigned char Seg8G         :1; //Seg8      LCD10
    unsigned char Seg9F         :1; //Seg9      LCD11
    unsigned char Seg9G         :1; //Seg10     LCD12
    unsigned char Seg10F        :1; //Seg11     LCD13   (LCD14 -> SEG29)
    unsigned char RPM           :1; //Seg12     LCD15
    unsigned char Flame         :1; //Seg13     LCD16
    unsigned char BarGraph_I7    :1; //Seg14     LCD17
    unsigned char BarFrame_I7   :1; //Seg15     LCD18    

    unsigned char Dummy15       :1; //Seg16     Disabled!
    unsigned char Dummy16       :1; //Seg17     Disabled!
    unsigned char Seg4C         :1; //Seg18     LCD19
    unsigned char Seg4E         :1; //Seg19     LCD20
    unsigned char Seg3C         :1; //Seg20     LCD21
    unsigned char Seg3E         :1; //Seg21     LCD22
    unsigned char Seg2C         :1; //Seg22     LCD23
    unsigned char Seg2E         :1; //Seg23     LCD24

    unsigned char Seg1C         :1; //Seg24     LCD25
    unsigned char Seg1E         :1; //Seg25     LCD26
    unsigned char Phone         :1; //Seg26     LCD27
    unsigned char Dummy17       :1; //Seg27     Disabled
    unsigned char Dummy18       :1; //Seg28     Disabled
    unsigned char Seg10G        :1; //Seg29     LCD14
    unsigned char Seg8F         :1; //Seg30     LCD9
    unsigned char Dummy19        :1; //Seg31     Disabled

    unsigned char Dummy20       :8; //LCDDATA16 not available
    unsigned char Dummy21       :8; //LCDDATA17 not available

    // -- COM 0 (LCD)-- //)         PIC(No) |   LCD(No))
    unsigned char Line          :1; //Seg0      LCD1
    unsigned char Seg5A         :1; //Seg1      LCD2
    unsigned char Seg5B         :1; //Seg2      LCD3
    unsigned char Seg6A         :1; //Seg3      LCD4
    unsigned char Seg6B         :1; //Seg4      LCD5
    unsigned char Seg7A         :1; //Seg5      LCD6
    unsigned char Seg7B         :1; //Seg6      LCD7
    unsigned char Dummy22       :1; //Seg7      LCD8    (LCD9 -> SEG30)

    unsigned char Seg8B         :1; //Seg8      LCD10
    unsigned char Seg9A         :1; //Seg9      LCD11
    unsigned char Seg9B         :1; //Seg10     LCD12
    unsigned char Seg10A        :1; //Seg11     LCD13   (LCD14 -> SEG29)
    unsigned char DEGUPPER      :1; //Seg12     LCD15
    unsigned char Dummy23       :1; //Seg13     LCD16
    unsigned char BarFrame_I8   :1; //Seg14     LCD17
    unsigned char BarGraph_I8   :1; //Seg15     LCD18    

    unsigned char Dummy24       :1; //Seg16     Disabled!
    unsigned char Dummy25       :1; //Seg17     Disabled!
    unsigned char BAR           :1; //Seg18     LCD19
    unsigned char Seg4D         :1; //Seg19     LCD20
    unsigned char DP4           :1; //Seg20     LCD21
    unsigned char Seg3D         :1; //Seg21     LCD22
    unsigned char DP2           :1; //Seg22     LCD23
    unsigned char Seg2D         :1; //Seg23     LCD24

    unsigned char DP1           :1; //Seg24     LCD25
    unsigned char Seg1D         :1; //Seg25     LCD26
    unsigned char DP3           :1; //Seg26     LCD27
    unsigned char Dummy26       :1; //Seg27     Disabled
    unsigned char Dummy27       :1; //Seg28     Disabled
    unsigned char Seg10B        :1; //Seg29     LCD14
    unsigned char Seg8A         :1; //Seg30     LCD9
    unsigned char Dummy28       :1; //Seg31     Disabled

    unsigned char Dummy29       :8; //LCDDATA22 not available
    unsigned char Dummy30       :8; //LCDDATA23 not available
} StructLCD;

typedef struct 
{
    unsigned char SEG_A :1;
    unsigned char SEG_B :1;
    unsigned char SEG_C :1;
    unsigned char SEG_D :1;
    unsigned char SEG_E :1;
    unsigned char SEG_F :1;
    unsigned char SEG_G :1;
    unsigned char NONE  :1;   
}DGSeg;

typedef union 
{
    unsigned char DigitChar;
    DGSeg DigitBit;   
}UDigit;

extern volatile StructLCD MediumLCD;
extern volatile UDigit UpdatedDigit;

void LcdInit(void);
void LCD_7SEG(void);
void LCD_Update(void);

#endif  /* LCD_DRIVER_H */

编辑:在变量定义之间添加printf行意外地起作用。如果我对这些行进行评论,则问题仍然存在。这种行为对我来说仍然没有意义。

Edit2:添加了完整代码。

void LCD_7SEG(void)
{
    unsigned char buffer= 0x11; // assign a value
    printf("%d\n", buffer);
    UpdatedDigit.DigitChar = buffer; // the value is passed successfully, LCD shows SEG_A and SEGE as expected.
    printf("%d\n", UpdatedDigit.DigitChar);      

编辑3:添加了unsigned char的定义。

<stdint.h>
#ifndef uint8_t
typedef unsigned char uint8_t;
#define uint8_t uint8_t
#define UINT8_MAX (255)
#endif

1 个答案:

答案 0 :(得分:0)

试试这个:

添加

void LCD_Set(int value)
{         
  UpdatedDigit.DigitChar = value;
}

修改

void LCD_7SEG(void)
{         
  LCD_Set(0xff);
}