0% found this document useful (0 votes)
17 views23 pages

Phao LTNHUNG

The document discusses displaying images and text on an LCD screen using different methods: 1. Displaying a single image or text on the LCD. 2. Displaying two images and switching between them using a button press. Circles and numbers are drawn to indicate each image. 3. Continuously displaying strings one after the other in a loop. Methods to move the strings from different directions are also discussed. 4. Toggle the state of 3 LEDs when a button is pressed by changing a status variable. Interrupts are used to detect the button press.

Uploaded by

Tính Tê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

Phao LTNHUNG

The document discusses displaying images and text on an LCD screen using different methods: 1. Displaying a single image or text on the LCD. 2. Displaying two images and switching between them using a button press. Circles and numbers are drawn to indicate each image. 3. Continuously displaying strings one after the other in a loop. Methods to move the strings from different directions are also discussed. 4. Toggle the state of 3 LEDs when a button is pressed by changing a status variable. Interrupts are used to detect the button press.

Uploaded by

Tính Tê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Bài 1 : Hiển thị chữ lên LCD

Bài 2 : Hiển thị ảnh


#include <stdint.h>

#include <stdio.h>

#include <system_stm32f4xx.h>

#include <stm32f401re_gpio.h>

#include <timer.h>

#include <Ucglib.h>

#include "ucg.h"

#include "Image_1.h"

static ucg_t ucg;

#define ROTATE_0 0

#define ROTATE_90 1

#define ROTATE_180 2

#define ROTATE_360 3

#define SIZE_IMAGE 128


void delay(uint16_t _ms){

uint32_t _timer = 16000;

while(_timer > 0){

while(_ms > 0)

_ms--;

_timer--;

int main(void)

SystemCoreClockUpdate();

TimerInit();

Ucglib4WireSWSPI_begin(&ucg, UCG_FONT_MODE_SOLID);

ucg_ClearScreen(&ucg);

ucg_SetFont(&ucg, ucg_font_helvR08_tf);

ucg_SetColor(&ucg, 0,0,0,255);

ucg_SetRotate180(&ucg);

while(1)

for(int i=0; i< SIZE_IMAGE; i++)

for(int j=0; j< SIZE_IMAGE; j++)

ucg_SetColor(&ucg, 0,image1[ SIZE_IMAGE*i*3 + j*3 +


2],image1[SIZE_IMAGE*i*3 + j*3+1],image1[SIZE_IMAGE*i*3 + j*3]);
ucg_DrawPixel(&ucg, j, i);

Hàm image

#ifndef IMAGE_1_H_
#define IMAGE_1_H_
const uint8_t image1[] = {
0xfb, 0xfe, 0xff, 0xfc, 0xfe, 0xff, 0xfd, 0xfc, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xfc,
0xfb, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xe9, 0xed, 0xee, 0xd7, 0xdc, 0xd8, 0xd8,
0xdf, 0xd8, 0xdc, 0xde, 0xdb, 0xdb, 0xdd, 0xdc, 0xdd, 0xdc, 0xe1, 0xd8, 0xd9,
0xdd, 0xd8, 0xd9, 0xdd, 0xfc, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xfa, 0xfe, 0xff, 0xff,
0xfe, 0xff, 0xff, 0xfd, 0xfe, 0xfb, 0xfb, 0xfd, 0xfc, 0xff, 0xff, 0xe9, 0xee, 0xf4,
0xd6, 0xdb, 0xdf, 0xdf, 0xdd, 0xe0, 0xdf, 0xdb, 0xdc, 0xe0, 0xdb, 0xe1, 0xdd,
0xda, 0xe1, 0xde, 0xe1, 0xe8, 0xd3, 0xd8, 0xde, 0xf9, 0xff, 0xff, 0xf7, 0xff, 0xfd,
0xf7, 0xff, 0xfb, 0xf7, 0xff, 0xfb, 0xf8, 0xfe, 0xfc, 0xfa, 0xff, 0xfe, 0xfb, 0xff,
0xfe, 0xea, 0xee, 0xed, 0xdc, 0xdd, 0xdf, 0xdb, 0xdc, 0xde, 0xd9, 0xd9, 0xdb,
0xe0, 0xe1, 0xe3, 0xdc, 0xde, 0xdd, 0xd6, 0xda, 0xd9, 0xdb, 0xe1, 0xdf, 0xfb,
0xff, 0xff, 0xf5, 0xfe, 0xf9, 0xfb, 0xff, 0xfd, 0xfd, 0xfb, 0xfc, 0xff, 0xfe, 0xff,
0xfd, 0xfd, 0xfb, 0xf9, 0xfe, 0xfa, 0xea, 0xf0, 0xee, 0xd4, 0xdd, 0xda, 0xd4, 0xdc,
0xde, 0xd8, 0xe0, 0xe2, 0xd8, 0xdc, 0xdd, 0xd8, 0xdc,
};

#endif /* IMAGE_1_H_ */

Bài 3 : Hiển thị 2 ảnh qua button

#include <stdint.h>
#include <stdio.h>
#include <system_stm32f4xx.h>
#include <led.h>
#include <stm32f401re_gpio.h>
#include <stm32f401re_rcc.h>
#include <button.h>
#include <timer.h>
#include <Ucglib.h>
#include "ucg.h"
#include "Image_2.h"
#include "Image_1.h"

static ucg_t ucg;

#define ROTATE_0 0
#define ROTATE_90 1
#define ROTATE_180 2
#define ROTATE_360 3
#define SIZE_IMAGE 128

static uint8_t flag_load = 0, Image = 0;

static void ButtonB2_Init(void){


GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void delay(uint16_t _ms){


uint32_t _timer = 16000;
while(_timer > 0){
while(_ms > 0)
_ms--;
_timer--;
}
}

int main(void)
{ //Buổi 1
SystemCoreClockUpdate();
ButtonB2_Init();
Ucglib4WireSWSPI_begin(&ucg, UCG_FONT_MODE_SOLID);
ucg_ClearScreen(&ucg);
ucg_SetFont(&ucg, ucg_font_helvR08_tf);
ucg_SetRotate180(&ucg);
ucg_DrawCircle(&ucg, 30,40,20,UCG_DRAW_ALL);
ucg_SetColor(&ucg,1,0,0,0);
ucg_DrawCircle(&ucg, 100,40,20,UCG_DRAW_ALL);
ucg_DrawString (&ucg,27,45,0, "1");
ucg_DrawString (&ucg, 97,45,0, "2");

while(1){

if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)==0)
{
flag_load = 1;
Image =! Image;
}

if((flag_load == 1)&&(Image == 1))


{
flag_load = 0;
for(int i=0; i< SIZE_IMAGE; i++)
{
for(int j=0; j< SIZE_IMAGE; j++)
{
ucg_SetColor(&ucg, 0,image2[ SIZE_IMAGE*i*3
+ j*3 + 2],image2[SIZE_IMAGE*i*3 + j*3+1],image2[SIZE_IMAGE*i*3 + j*3]);
ucg_DrawPixel(&ucg, j, i);
}

}
}
else if((flag_load == 1)&&(Image == 0))
{
flag_load = 0;
for(int i=0; i< SIZE_IMAGE; i++)
{
for(int j=0; j< SIZE_IMAGE; j++)
{
ucg_SetColor(&ucg, 0,image1[ SIZE_IMAGE*i*3
+ j*3 + 2],image1[SIZE_IMAGE*i*3 + j*3+1],image1[SIZE_IMAGE*i*3 + j*3]);
ucg_DrawPixel(&ucg, j, i);
}

}
}
}
}

- Hai hàm img1 và img 2:

Bài 4 : chuỗi hiển thị liên tiếp


- Để không hiển thị lại chuỗi đầu tiên sau khi hiển thị chuỗi cuối comment lại
TimeStart displayString1 trong hàm displayString3

Bài 5: Di chuyển 4 chuỗi khác nhau từ 4 phía vào

#include <stdint.h>
#include <system_stm32f4xx.h>
#include <stm32f401re_gpio.h>
#include <timer.h>
#include <Ucglib.h>
static ucg_t ucg;

#define DISPLAY_WIDTH 16
#define DISPLAY_HEIGHT 8

const char* strings[] = {


"B19DCVT103",
"TRAN ANH DUC",
"NGO THU TRANG",
"OPENAI"
};
const uint8_t numStrings = sizeof(strings) / sizeof(strings[0]);

void clearScreen(void) {
ucg_ClearScreen(&ucg);
}

void displayString(const char* string, int8_t x, int8_t y) {


ucg_SetFont(&ucg, ucg_font_helvR08_tf);
ucg_DrawString(&ucg, x, y, 0, string);
}

int main(void) {
SystemCoreClockUpdate();
TimerInit();

Ucglib4WireSWSPI_begin(&ucg, UCG_FONT_MODE_SOLID);
ucg_ClearScreen(&ucg);
ucg_SetColor(&ucg, 0, 255, 255, 255);

uint8_t stringIndex = 0;
int8_t stringX = 0;
int8_t stringY = 0;

while (1) {
clearScreen();
displayString(strings[stringIndex], stringX, stringY);

stringIndex = (stringIndex + 1) % numStrings;

switch (stringIndex) {
case 0: // Hiển thị từ bên trái qua phải
stringX++;
if (stringX >= DISPLAY_WIDTH) {
stringX = -ucg_GetStrWidth(&ucg, strings[stringIndex]);
}
break;
case 1: // Hiển thị từ bên phải qua trái
stringX--;
if (stringX <= -ucg_GetStrWidth(&ucg, strings[stringIndex])) {
stringX = DISPLAY_WIDTH;
}
break;
case 2: // Hiển thị từ trên xuống dưới
stringY++;
if (stringY >= ucg_GetFontAscent(&ucg) + DISPLAY_HEIGHT) {
stringY = -ucg_GetFontDescent(&ucg);
}
break;
case 3: // Hiển thị từ dưới lên trên
stringY--;
if (stringY <= -ucg_GetFontDescent(&ucg)) {
stringY = ucg_GetFontAscent(&ucg) + DISPLAY_HEIGHT;
}
break;
}

processTimerScheduler();
}
}

Bài 6 : Đảo trạng thái đầu ra của 3 led khi nhấn Button

#include <stdint.h>
#include <stdio.h>
#include <system_stm32f4xx.h>
#include <stm32f401re_gpio.h>
#include <stm32f401re_rcc.h>
#include <stm32f401re_gpio.h>
#include <stm32f401re_exti.h>
#include <stm32f401re_syscfg.h>
#include <misc.h>
#include <timer.h>
#include <Ucglib.h>
#include "ucg.h"

#define GPIO_PIN_SET 1
#define GPIO_PIN_RESET 0
#define GPIO_PIN_HIGH 1
#define GPIO_PIN_LOW 0

#define LED_GPIO_PORT GPIOA


#define LED_GPIO_PIN_0 GPIO_Pin_0
#define LED_GPIO_PIN_5 GPIO_Pin_5
#define LED_GPIO_PIN_10 GPIO_Pin_10
#define LEDControl_SetClock RCC_AHB1Periph_GPIOA

#define BUTTON_GPIO_PORT GPIOA


#define BUTTON_GPIO_PIN GPIO_Pin_4
#define BUTTONControl_SetClock RCC_AHB1Periph_GPIOA

uint8_t Status = 0; // Giá trị ban đầu = 0


static void Led_Init(void);
static void delay(void);
static void Interrupt_Init(void);
static void LedControl_Status(GPIO_TypeDef* GPIOx, uint16_t GPIO_PIN,
uint8_t Status);

void EXTI4_IRQHandler(void)
{
if (EXTI_GetFlagStatus(EXTI_Line4) == SET) // Kiểm tra chân ngắt EXTI4 đã
được kích hoạt hay chưa
{
Status = !Status; // Đảo ngược trạng thái của biến Status khi có tín hiệu ngắt
}
EXTI_ClearITPendingBit(EXTI_Line4); // Xóa cờ ngắt để tiếp tục thực thi các
lệnh tiếp theo
}

int main()
{
SystemCoreClockUpdate(); // Cập nhật tần số hoạt động của hệ thống
Led_Init(); // Khởi tạo chức năng điều khiển đèn LED trên vi điều khiển
Interrupt_Init(); // Khởi tạo ngắt

while (1)
{
if (Status == 0)
{
delay();
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_0,
GPIO_PIN_RESET); // Đưa trạng thái LED về 0 nghĩa là tắt đèn
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_5,
GPIO_PIN_RESET);
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_10,
GPIO_PIN_RESET);
}
else
{
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_0,
GPIO_PIN_HIGH);
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_5,
GPIO_PIN_HIGH);
LedControl_Status(LED_GPIO_PORT, LED_GPIO_PIN_10,
GPIO_PIN_HIGH);
}
}

return 0;
}

static void Led_Init(void)


{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(LEDControl_SetClock, ENABLE);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN_0 | LED_GPIO_PIN_5 |
LED_GPIO_PIN_10;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}

// ...

static void delay(void)


{
uint32_t delay_count = 1000000; // Số lần lặp để tạo độ trễ (số lớn tùy thuộc vào
tốc độ vi điều khiển)

for (uint32_t i = 0; i < delay_count; i++)


{
// Chờ
}
}
static void Interrupt_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource4);

EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; // Đặt chế độ ngắt
khi có cạnh dương
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void LedControl_Status(GPIO_TypeDef* GPIOx, uint16_t GPIO_PIN,
uint8_t Status)
{
if (Status == GPIO_PIN_RESET)
{
GPIO_ResetBits(GPIOx, GPIO_PIN);
}
else if (Status == GPIO_PIN_HIGH)
{
GPIO_SetBits(GPIOx, GPIO_PIN);
}
}

Bài 7: đảo trạng thái đầu ra của 1 led khi nhấn button

#include <stdint.h>
#include "stm32f401re_rcc.h"
#include "stm32f401re_gpio.h"

#define GPIO_PIN_SET 1
#define GPIO_PIN_RESET 0
#define GPIO_PIN_HIGH 1
#define GPIO_PIN_LOW 0

#define LED_GPIO_PORT GPIOA


#define LED_GPIO_PIN GPIO_Pin_1
#define LEDControl_SetClock RCC_AHB1Periph_GPIOA

#define BUTTON_GPIO_PORT GPIOB


#define BUTTON_GPIO_PIN GPIO_Pin_3
#define BUTTONControl_SetClock RCC_AHB1Periph_GPIOB

static void delay(void);


static void Led_Init(void);
static void Button_Init(void);
static void LedControl_Status(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN,
uint8_t Status);

int main() {
SystemCoreClockUpdate();
Led_Init();
Button_Init();
uint8_t ledStatus = 0;

while (1) {
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT,
BUTTON_GPIO_PIN) == GPIO_PIN_RESET) { //kiem tra neu nut nhan dc nhan
delay();
if (GPIO_ReadInputDataBit(BUTTON_GPIO_PORT,
BUTTON_GPIO_PIN) == GPIO_PIN_RESET) {//neu lon hon thoi gian tre ma nut
van nhan
// Button is pressed
ledStatus = !ledStatus; //dao trang
thai ledstatus (ledstatus=0 => tat, ledstatus=1 => bat)
LedControl_Status(LED_GPIO_PORT,
LED_GPIO_PIN, ledStatus); //ghi trang thai led ra chan gpio
// Wait for button to be released
while
(GPIO_ReadInputDataBit(BUTTON_GPIO_PORT, BUTTON_GPIO_PIN) ==
GPIO_PIN_RESET); //dung lai cho den khi nut nhan dc tha ra (neu tha ra thi
quay lai doan dong 30 kiem tra xem khi nao no dc nhan)
}
}
}
}

static void Led_Init(void) {


GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(LEDControl_SetClock, ENABLE);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}
static void Button_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(BUTTONControl_SetClock, ENABLE);

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Pin = BUTTON_GPIO_PIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(BUTTON_GPIO_PORT, &GPIO_InitStructure);
}

static void LedControl_Status(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN,


uint8_t Status) {
if (Status == GPIO_PIN_SET) {
GPIOx->BSRRL = GPIO_PIN;
}
if (Status == GPIO_PIN_RESET) {
GPIOx->BSRRH = GPIO_PIN;
}
}
static void delay(void)
{
for(uint32_t i = 0; i < 500000; i++);
}
/*
* Thanh ghi BSRRL (Bit Set Reset Register Low) được sử dụng để bật các chân
GPIO. Khi một bit trong thanh ghi này được ghi là 1, tương ứng với chân GPIO
tương ứng, thì chân đó sẽ được bật.

Thanh ghi BSRRH (Bit Set Reset Register High) được sử dụng để tắt các chân
GPIO. Khi một bit trong thanh ghi này được ghi là 1, tương ứng với chân GPIO
tương ứng, thì chân đó sẽ được tắt.
* */

You might also like