Display time on niosii LCD

Source: Internet
Author: User

This article is reproduced in: workshop!

Display the time, date, and week on lcd1602

Because SDRAM is used, first we add a PLL

Input clock 50 MHz

The output clock C0 is 50 MHz and the phase is-75 degrees. The clock is provided to the SDRAM and the pin is named sd_clk.

C1 50nhz, with a phase of 0 degrees. It provides a clock signal to the nioss-clk and is connected to the nioss-clk.

1. enable the system file system, add the CPU, and select the system file system.

2. Add SDRAM, select Custom for presets, and set data width to 16. If the other values do not change, click Finish ......

3. Add timer, period: 1 s, presets: full-featured

4. Add a character LCD with no parameter settings

5. Add PIO, width: 4, input

The second parameter sets the screen, dropping along the capture, and edge interruption

Renamed as key

6. The CPU Reset vector and exception vector are both SDRAM

7. Auto-assign base addresses and auto-assign irqs, then generate ......

8. In the schematic diagram, add the System File System system to the System File System and change the pin name. For example:

9. Pin locking, based on the actual situation

10. Start compilation and progammer

11. niosiiCode:

# include "system. H "// contains basic hardware description information
# include" altera_avalon_timer_regs.h "// defines the ing of kernel registers, provide symbolic access to underlying hardware
# include "altera_avalon_pio_regs.h" // contains basic I/O port information
# include "alt_types.h" // the data type defined by Altera
# include "sys/alt_irq.h"
# include "unistd. H "// delay function usleep
# include" stdio. H "
// define the port
# define key * (volatile unsigned char *) key_base

// Function declaration
Void timer_init ();
Void timer_isr (void * context, alt_u32 ID );
Void display ();
Void keydown_isr (void * context, alt_u32 ID );
Void keydown_init ();

// Variable definition
Alt_u8 num_s [8] = {0, 0xbf, 0, 0, 0xbf, 0, 0}; // time displayed for each user
Alt_u8 num_d [8] = {0, 0, 0, 0, 0, 0}; // display each date
Alt_u8 second, Min, hour, day, month, weekday;
Alt_2010year;

Char * week [] = {"sun", "mon", "Tue", "wed", "Thu", "fri", "sat"}; // week

Void init_value (void)
{
Second = 0;
Min = 0;
Hour = 12;
Day = 1;
Month = 1;
Year = 2010;
Weekday = 0;
}

// Key interrupt service function
Void keydown_isr (void * context, alt_u32 ID)
{
If (Key = 0x06) {second = 0; while (Key = 0x06) {display () ;}// second call settings
If (Key = 0x05) {min ++; If (min = 60) min = 0; while (Key = 0x05) {display () ;}// tune settings
If (Key = 0x03) {hour ++; If (hour = 24) hour = 0; while (Key = 0x03) {display () ;}// hour settings
If (Key = 0x0e) // Date settings
{
Day ++;
If (month <= 7 & month % 2) | (month> = 8 &&(! (Month % 2) // 31 days of the month
{
If (Day = 32) Day = 1;
}
Else // 30 months
{
If (month = 2) // whether January 1, February is 28 days or 29 days
{
If ((! (Year % 4) & amp; Year % 100) | (! (Year % 400) // 29 days of the leap year
{If (Day = 30) Day = 1 ;}
Else if (Day = 29) Day = 1; // 28 days per year
}
Else if (Day = 31) Day = 1;
}
While (Key = 0x0e) display ();
}
If (Key = 0x0c) {weekday ++; If (weekday = 7) weekday = 0; while (Key = 0x0c) {display ();}} // month settings
If (Key = 0x0d) {month ++; If (month = 13) month = 1; while (Key = 0x0d) {display ();}} // month settings
If (Key = 0x0b) {year --; If (year = 1900) year = 2010; while (Key = 0x0b) {display ();}} // year setting, decreasing
If (Key = 0x08) {year ++; If (year = 2100) year = 2010; while (Key = 0x08) {display () ;}// year setting, incrementing

Iowr_altera_avalon_pio_edge_cap (key_base, 0x00); // clear the interrupt capture register
}

// Key interrupt Initialization
Void keydown_init ()
{
Iowr_altera_avalon_pio_irq_mask (key_base, 0x07); // enable key interruption
Iowr_altera_avalon_pio_edge_cap (key_base, 0x00); // clear the interrupt capture register
Alt_irq_register (key_irq, 0, keydown_isr); // registers the interrupt function
}

// Timer Initialization
Void timer_init ()
{
Alt_irq_register (timer_irq, 0, timer_isr); // registers the interrupt function
Iowr_altera_avalon_timer_status (timer_base, 0); // clear the status flag
Iowr_altera_avalon_timer_control (timer_base, 7); // start the timer to allow interruption and count continuously
}

// The timer interrupts the service function.
Void timer_isr (void * context, alt_u32 ID)
{
Second ++;
If (second = 60) {min ++; second = 0 ;}
If (min = 60) {hour ++; min = 0 ;}
If (hour = 24) {day ++; weekday ++; hour = 0 ;}
If (weekday = 7) weekday = 0;
If (month <= 7 & month % 2) | (month> = 8 &&(! (Month % 2 ))))
{
If (Day = 32)
{
Day = 1;
Month ++;
}
}
Else
{
If (month = 2)
{
If ((! (Year % 4) & amp; Year % 100) | (! (Year % 400 )))
{
If (Day = 30)
{
Day = 1;
Month ++;
}
}
Else if (Day = 29)
{
Day = 1;
Month ++;
}
}
Else if (Day = 31)
{
Day = 1;
Month ++;
}
}
If (month = 13) {month = 1; year ++ ;}
If (year = 2100) year = 2010;
// If (second % 2) Duan [10] = 0xff;
// Else Duan [10] = 0xbf; // flashes in seconds
Iowr_altera_avalon_timer_status (timer_base, 0); // clear Status Register
}

// Digital display function
Void display ()
{
Num_s [0] = Second % 10;
Num_s [1] = Second/10;
Num_s [2] = 10;
Num_s [3] = min % 10;
Num_s [4] = min/10;
Num_s [5] = 10;
Num_s [6] = hour % 10;
Num_s [7] = hour/10;
Num_d [0] = day % 10;
Num_d [1] = day/10;
Num_d [2] = month % 10;
Num_d [3] = month/10;
Num_d [4] = year % 10;
Num_d [5] = year/10% 10;
Num_d [6] = year/100% 10;
Num_d [7] = year/1000;
Printf ("% d-% d \ n % d-% d % s \ N ",
Num_d [7], num_d [6], num_d [5], num_d [4], num_d [3], num_d [2], num_d [1], num_d [0],
Num_s [7], num_s [6], num_s [4], num_s [3], num_s [1], num_s [0], week [weekday]);
// Display lcd6102
}

// Main Function
Int main (void)
{
Keydown_init (); // key interrupt Initialization
Timer_init (); // timer Initialization
Init_value ();
While (1)
{
Display (); // display
}
Return 0;
}

It is difficult to repeat the key and write the code according to the actual situation ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.