March of the Text, . . .

The last project we worked on was just a flashing LED. For this next project we are going add a few things

  1. Change the PIC to a PIC16F1827
  2. Introduce an LCD
  3. Display text and move the text from side to side
Salvo [FINIS]

Salvo [FINIS]

What to look forward to, . . .

In an upcoming project(s) I will introduce analogue to digital conversion to measure temperature, and PWM.

The PIC16F1827

This device is a member of the F1xx family from Microchip. I am switching to this device from the PIC16F628 because the X628 has analogue comparators but no A2D conversion capability. This device however, has much more peripheral packed internally which also means there are more registers that you need to configure before the device can be used.

NOTE: By default, when the device is reset the device does not default to digital I/O but rather to one of the alternative peripherals. See the following page for a list of I/O proiorities on reset.

For a complete list of available peripherals and the PIN or PINs to which they are connected, see page : 6 of the following datasheet. Microchip PIC16F1827

For this project, the project needs to be configured for a change in device, namely the PIC16F1827. Once this is done you can either update the code or copy and paste the new code in. I would suggest the latter.

You will notice a number of new methods in this new code segment.

I have worked some time with .NET and one thing they teach you is that each method does one thing. This can at times conflict with sequential programming, if that is what you are familair with. You will notice that I have broken the code down into a number of methods which each one returning either nothing, or performing singular tasks. This can make the code harder to read or follwo becuase we don’t have the more advanced IDE to make things easier for us. This method does however make trouble shooting easier, because code segments can be excluded from running to assist in diagnosing issues.

NB: A big NB on this is that PICs only do what they are programmed to do. As a result if it does not do what it’s suppose to do, that means that either the code is wrong, or the connections are wrong.

 

void IOConfig

In the method below PORTB and PORTA are configured to

  1. causes the LED to flash,
  2. configure across the two ports the necessary I/O lines to operate the LCD.

PORTB 0 | 1 have not been used for anything pivotal (the LED cna be moved to another PIN) because the UART is connected to these pins and these will be used in a later project.

Since at this time I am not going to use the analogues, they will be disabled for both ports. The register(s) ANSEL[n] is used to configure which PINS are used for analogue. Keep in mind that if the PIN is configured for analogue then the TRIS register must be configured as an input as well.

The void IOConfig() method does the following

  1.  Configures port direction
  2. Disables the analogues
  3. disables the comparators 1 and 2.

void LCDConfig()

The LCD data and control lines are initialised at the top of the code and the method void LCDConfig() initialises the LCD and places a single line of text on the top line.

The I/O and data line intilisation is listed further up in the code and is essential to initialise the device.

 

void main()

The main loop entry point calls the above two methods that I discussed to configure the device’s operation. Once configured, the code enters a continuouse loop that moves the LCD text left and right. A small trick that I have employed here is to use the LED flashing duration as the pause between each left or right move of the line of text. This gives the project good synergy and user flow.

Once you have connected up the required hardware, programmed the code the text will appear on the screen and march from left to right.

NOTE: All code examples are written with mikro C PRO for PIC.

// LCD module connections

sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;                 // sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;   // sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

char LCDTXT[] = "Salvo[FINIS]";
char LCDMSG[] = "Data";
char uart_rd;
unsigned short new_DC, current_DC;
unsigned int ADC_Value = 0;
char *temp = "0000";
int i=0;

/*
  Configures system values for startup
*/

void IOConfig()
{
     // Disable Analog

     ANSELA = 0x0;
     ANSELB = 0x0;

     // Ports

     TRISB = 0xF8;
     TRISA = 0x0;

     // PIC16F1827 has two capture compare modules and both need to be disabled

     CCP1CON = 0x0;
     CCP2CON = 0x0;
}

void LCDConfig()
{
     Lcd_Init();                                // Initialize LCD

     Lcd_Cmd(_LCD_CLEAR);                       // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);                  // Cursor off
     Lcd_Out(1,1,LCDTXT);                       // Write text in first row
}

 void LED()
{
      PORTB.B0 = 1;
      Delay_ms(250);
      PORTB.B0 = 0;
      Delay_ms(250);
}

void main() {

     IOConfig();

     LCDConfig();

     while(1)
     {

             // LED();                        // Test display signal

             for(i=0; i<4; i++) {             // Move text to the right 4 times
                    Lcd_Cmd(_LCD_SHIFT_RIGHT);
                    LED();
                 }

             for(i=0; i<4; i++) {             // Move text to the left 4 times
                    Lcd_Cmd(_LCD_SHIFT_LEFT);
                    LED();
                  }
     }
}

Project Resources

The project hex file can be downloaded here: Salvo [1827]

 

Summary

This project is quite a simple application, and from a practical perspective it is mainly hardware testing. In the next article I will introduce some more useful items to make it more useful.

[E]