You are on page 1of 8

F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.

c Thursday, September 25, 2014 4:29 PM


/*
* Copyright (c) 2009 Xilinx, Inc. All rights reserved.
*
* Xilinx, Inc.
* XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
* COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
* ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
* STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
* IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
* FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
* XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
* THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
* ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
* FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#include "xparameters.h"
#include "xbasic_types.h"
#include "xgpio.h"
#include "xstatus.h"
#include "xtmrctr.h"
#include "multiplier.h"
// Masks to the pins on the GPIO port
#define LCD_DB4 0x01
#define LCD_DB5 0x02
#define LCD_DB6 0x04
#define LCD_DB7 0x08
#define LCD_RW 0x10
#define LCD_RS 0x20
#define LCD_E 0x40
#define LCD_TEST 0x80
#define MULTIPLIER_USER_SLV_SPACE_OFFSET (0x00000000)
#define MULTIPLIER_SLV_REG0_OFFSET (MULTIPLIER_USER_SLV_SPACE_OFFSET + 0x00000000)
#define MULTIPLIER_SLV_REG1_OFFSET (MULTIPLIER_USER_SLV_SPACE_OFFSET + 0x00000004)
#define MULTIPLIER_SLV_REG2_OFFSET (MULTIPLIER_USER_SLV_SPACE_OFFSET + 0x00000008)
#define MULTIPLIER_SLV_REG3_OFFSET (MULTIPLIER_USER_SLV_SPACE_OFFSET + 0x0000000C)
# define BaseAddress 0xcc600000
// Global variables
XGpio GpioOutput;
XGpio GpioInput;
XTmrCtr DelayTimer;
// Function prototypes
-1-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
void delay_us(Xuint32 time); // delays in micro seconds
void delay_ms(Xuint32 time); /// delays in mili second .
void gpio_write(Xuint32 c); // write in gpio
Xuint32 gpio_read(void); // read from gpio
void lcd_clk(void); // clock of LCD.
void lcd_set_test(void); // lcd set_test
void lcd_reset_test(void); // lcd reset test
void lcd_set_rs(void);
void lcd_reset_rs(void);
void lcd_set_rw(void);
void lcd_reset_rw(void);
void lcd_write(Xuint32 c); /// lcd write
void lcd_clear(void); // lcd clear
void lcd_puts(const char * s); // lcd print function
void lcd_putch(Xuint32 c); // print character
void lcd_goto(Xuint32 line,Xuint32 pos); //position of LCD
void lcd_init(void); // lcd initialization
// Main function
int main (void)
{
Xuint32 status;
// Clear the screen
//xil_printf("%c[2J",27);
//xil_printf("16x2 LCD Driver by Virtex-5 Resource\r\n");
// xil_printf("http://www.fpgadeveloper.com\r\n");
// Initialize the Timer
status = XTmrCtr_Initialize(&DelayTimer,
XPAR_XPS_TIMER_0_DEVICE_ID);
if (status != XST_SUCCESS){
xil_printf("Timer failed to initialize\r\n");
return XST_FAILURE;
}
XTmrCtr_SetOptions(&DelayTimer, 1, XTC_DOWN_COUNT_OPTION);
// Initialize the GPIO driver for the LCD
status = XGpio_Initialize(&GpioOutput,
XPAR_XPS_GPIO_0_DEVICE_ID);
if (status != XST_SUCCESS){
xil_printf("GPIO failed to initialize\r\n");
return XST_FAILURE;
}
// Set the direction for all signals to be outputs
XGpio_SetDataDirection(&GpioOutput, 1, 0x00);
// Initialize the LCD
lcd_init();
-2-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
// Example write to the LCD
lcd_puts(" CEERI RCS LAB ");
lcd_goto(1,2);
lcd_puts("Result=");
// Xuint32 status1;
Xuint32 DataRead;
Xuint32 OldData;
int a = 0;
int b = 0;
int c;
// Initialize the GPIO driver so that it's ready to use,
status = XGpio_Initialize(&GpioInput,
XPAR_PUSH_BUTTONS_5BIT_DEVICE_ID);
if (status != XST_SUCCESS)
return XST_FAILURE;
// Set the direction for all signals to be inputs
XGpio_SetDataDirection(&GpioInput, 1, 0xFFFFFFFF);
OldData = 0x10;
while(1){
// Read the state of the DIP switches
DataRead = XGpio_DiscreteRead(&GpioInput, 1);
//a =a+1;
// b = b+1;
// Send the data to the UART if the settings change
if(DataRead == OldData){
//xil_printf("DIP Switch settings: 0x%X\r\n", DataRead);
lcd_clear();
a = a+1;
b = b+1;
lcd_puts(" CEERI RCS LAB ");
-3-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
lcd_goto(1,2);
lcd_puts("Result=");
xil_printf("result = ");
xil_printf("%d\n",c);
put_num(c);
delay_ms(500);
}
// Set the GPIO outputs to the DIP switch values
// XGpio_DiscreteWrite(&GpioOutput, 1, DataRead);
// Record the DIP switch settings
//OldData = DataRead;
MULTIPLIER_mWriteSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG0_OFFSET, 0);
MULTIPLIER_mWriteSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG1_OFFSET, a);
MULTIPLIER_mWriteSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG2_OFFSET, b);
MULTIPLIER_mWriteSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG0_OFFSET, 1);
c = MULTIPLIER_mReadSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG3_OFFSET);
MULTIPLIER_mWriteSlaveReg0(BaseAddress, MULTIPLIER_SLV_REG0_OFFSET, 0);
}
}
// Delay function (microseconds)
void delay_us(Xuint32 time)
{
XTmrCtr_SetResetValue(&DelayTimer, 1, time * 125);
XTmrCtr_Start(&DelayTimer, 1);
-4-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
while(!(XTmrCtr_IsExpired(&DelayTimer, 1))){}
XTmrCtr_Stop(&DelayTimer, 1);
}
// Delay function (milliseconds)
void delay_ms(Xuint32 time)
{
XTmrCtr_SetResetValue(&DelayTimer, 1, time * 125000);
XTmrCtr_Start(&DelayTimer, 1);
while(!(XTmrCtr_IsExpired(&DelayTimer, 1))){}
XTmrCtr_Stop(&DelayTimer, 1);
}
// Write to GPIO outputs
void gpio_write(Xuint32 c)
{
// Write to the GP IOs
XGpio_DiscreteWrite(&GpioOutput, 1, c & 0x0FF);
}
// Read the GPIO outputs
Xuint32 gpio_read()
{
// Read from the GP IOs
return(XGpio_DiscreteRead(&GpioOutput, 1));
}
// Clock the LCD (toggles E)
void lcd_clk()
{
Xuint32 c;
// Get existing outputs
c = gpio_read();
delay_us(1);
// Assert clock signal
gpio_write(c | LCD_E);
delay_us(1);
// Deassert the clock signal
gpio_write(c & (~LCD_E));
delay_us(1);
}
// Assert the RS signal
void lcd_set_rs()
{
Xuint32 c;
// Get existing outputs
c = gpio_read();
// Assert RS
gpio_write(c | LCD_RS);
delay_us(1);
}
-5-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
// Deassert the RS signal
void lcd_reset_rs()
{
Xuint32 c;
// Get existing outputs
c = gpio_read();
// Assert RS
gpio_write(c & (~LCD_RS));
delay_us(1);
}
// Assert the RW signal
void lcd_set_rw()
{
Xuint32 c;
// Get existing outputs
c = gpio_read();
// Assert RS
gpio_write(c | LCD_RW);
delay_us(1);
}
// Deassert the RW signal
void lcd_reset_rw()
{
Xuint32 c;
// Get existing outputs
c = gpio_read();
// Assert RS
gpio_write(c & (~LCD_RW));
delay_us(1);
}
// Write a byte to LCD (4 bit mode)
void lcd_write(Xuint32 c)
{
Xuint32 temp;
// Get existing outputs
temp = gpio_read();
temp = temp & 0xF0;
// Set the high nibble
temp = temp | ((c >> 4) & 0x0F);
gpio_write(temp);
// Clock
lcd_clk();
// Delay for "Write data into internal RAM 43us"
delay_us(100);
// Set the low nibble
temp = temp & 0xF0;
temp = temp | (c & 0x0F);
gpio_write(temp);
// Clock
-6-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
lcd_clk();
// Delay for "Write data into internal RAM 43us"
delay_us(100);
}
// Clear LCD
void lcd_clear(void)
{
lcd_reset_rs();
// Clear LCD
lcd_write(0x01);
// Delay for "Clear display 1.53ms"
delay_ms(2);
}
// Write a string to the LCD
void lcd_puts(const char * s)
{
lcd_set_rs();
while(*s)
lcd_write(*s++);
}
// Write character to the LCD
void lcd_putch(Xuint32 c)
{
lcd_set_rs();
lcd_write(c);
}
// Change cursor position
// (line = 0 or 1, pos = 0 to 15)
void lcd_goto(Xuint32 line, Xuint32 pos)
{
lcd_reset_rs();
pos = pos & 0x3F;
if(line == 0)
lcd_write(0x80 | pos);
else
lcd_write(0xC0 | pos);
}
// Initialize the LCD
void put_num(unsigned int num)
{
unsigned int k=10000,c=0,i;
while(c<5)
{
i=num/k;
num=num%k;
k/=10;
c++;
-7-
F:\task\source\lab1\lcd_puss_lcdf\hello_world_0\src\helloworld.c Thursday, September 25, 2014 4:29 PM
lcd_putch(i + 0x30);
}
}
void lcd_init(void)
{
Xuint32 temp;
// Write mode (always)
lcd_reset_rw();
// Write control bytes
lcd_reset_rs();
// Delay 15ms
delay_ms(15);
// Initialize
temp = gpio_read();
temp = temp | LCD_DB5;
gpio_write(temp);
lcd_clk();
lcd_clk();
lcd_clk();
// Delay 15ms
delay_ms(15);
// Function Set: 4 bit mode, 1/16 duty, 5x8 font, 2 lines
lcd_write(0x28);
// Display ON/OFF Control: ON
lcd_write(0x0C);
// Entry Mode Set: Increment (cursor moves forward)
lcd_write(0x06);
// Clear the display
lcd_clear();
}
-8-

You might also like