Last active
May 5, 2019 19:42
-
-
Save tranphuquy19/e6b44a75420e67dbf7587a5ba786298a to your computer and use it in GitHub Desktop.
THỰC HÀNH VI ĐIỀU KHIỂN STM32L1XX (LAB1, LAB5) [LED BLINK BUTTON, LED DELAY)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CHÚ Ý: CODE CHUẨN NẰM Ở DÒNG 334, LAB1: pg.335, LAB5 v.1 pg.388, LAB5 v.2 pg.498, LAB6 pg.581 | |
////******************** (C) Yifeng ZHU ******************** | |
//// @file main.c | |
//// @author Yifeng Zhu | |
//// @version V1.0.0 | |
//// @date November-11-2012 | |
//// @note | |
//// @brief C code for STM32L1xx Discovery Kit | |
//// @note | |
//// This code is for the book "Embedded Systems with ARM Cortex-M3 | |
//// Microcontrollers in Assembly Language and C, Yifeng Zhu, | |
//// ISBN-10: 0982692625. | |
//// @attension | |
//// This code is provided for education purpose. The author shall not be | |
//// held liable for any direct, indirect or consequential damages, for any | |
//// reason whatever. More information can be found from book website: | |
//// http://www.eece.maine.edu/~zhu/book | |
////******************************************************** | |
//#include <stdint.h> | |
///* Standard STM32L1xxx driver headers */ | |
//#include "stm32l1xx.h" | |
///* STM32L1xx Discovery Kit: | |
// - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK RCC_AHBENR_GPIOAEN | |
// - RESET Pushbutton: connected RESET | |
// - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK RCC_AHBENR_GPIOBEN | |
// - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK RCC_AHBENR_GPIOBEN | |
// - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0, PB1 (group 3) | |
//*/ | |
////****************************************************************************************** | |
////* The main program starts here | |
////****************************************************************************************** | |
//#define start ((unsigned int *) 0x20000000) | |
//#define target1 ((unsigned int *) 0x2000008) | |
//#define target2 ((unsigned int *) 0x20000010) | |
//int main(void){ | |
//// // Enable High Speed Internal Clock (HSI) | |
//// RCC->CR |= ((uint32_t)RCC_CR_HSION); | |
//// | |
//// // wait until HSI is ready | |
//// while ( (RCC->CR & (uint32_t) RCC_CR_HSIRDY) == 0 ) {;} | |
//// | |
//// // Select HSI as system clock source | |
//// RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); | |
//// RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI; | |
//// // Wait till HSI is used as system clock source | |
//// while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) == 0 ) {;} | |
//// | |
//// // Enable the clock to GPIO Port B | |
//// RCC->AHBENR |= 0x00000002; | |
//// GPIOB->MODER &= ~(0x03<<(2*6)) ; // Clear bit 13 and bit 12 | |
// // Dead loop & program hangs here | |
// *start = 0xFFFFFFFF; | |
// *target1 = 0; | |
// *target2 = 0xFFFFFF11; | |
// | |
// *start &= ~0x12; //detele bit index 1, 4 of $start | |
// *target1 |= 0x49; //set bit 0, 3, 6 | |
// *target2 ^= 0x1F; //~~ bit 0 1 2 3 4 target2 | |
// | |
// int temp; | |
// | |
// if(*start & (1<<31)) | |
// temp = 1; | |
// else temp = 2; | |
// while(1); | |
//} | |
//******************** (C) Yifeng ZHU ******************** | |
// @file main.c | |
// @author Yifeng Zhu | |
// @version V1.0.0 | |
// @date November-11-2012 | |
// @note | |
// @brief C code for STM32L1xx Discovery Kit | |
// @note | |
// This code is for the book "Embedded Systems with ARM Cortex-M3 | |
// Microcontrollers in Assembly Language and C, Yifeng Zhu, | |
// ISBN-10: 0982692625. | |
// @attension | |
// This code is provided for education purpose. The author shall not be | |
// held liable for any direct, indirect or consequential damages, for any | |
// reason whatever. More information can be found from book website: | |
// http://www.eece.maine.edu/~zhu/book | |
//******************************************************** | |
//SESSION2 Led Blind (not PWM) | |
//#include <stdint.h> | |
///* Standard STM32L1xxx driver headers */ | |
//#include "stm32l1xx.h" | |
///* STM32L1xx Discovery Kit: | |
// - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK RCC_AHBENR_GPIOAEN | |
// - RESET Pushbutton: connected RESET | |
// - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK RCC_AHBENR_GPIOBEN | |
// - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK RCC_AHBENR_GPIOBEN | |
// - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0, PB1 (group 3) | |
//*/ | |
//// Enable the clock to GPIO Ports A&B | |
//void GPIO_Clock_Enable() { | |
// RCC->AHBENR |= RCC_AHBENR_GPIOAEN; | |
// RCC->AHBENR |= RCC_AHBENR_GPIOBEN; | |
//} | |
///* Init a pin of Port B as digital output, output type of push-pull */ | |
//void GPIO_PortB_Output_Pin_Init(int pinID) { | |
// // Set pin as digital output (01) | |
// GPIOB->MODER &= ~(0x03<<(2*pinID)); | |
// GPIOB->MODER |= 0x01<<(2*pinID); | |
// | |
// // Set output type of pin as push-pull (0) | |
// GPIOB->OTYPER &= ~(0x01<<pinID); | |
// | |
// // Set pin's output speed (01 = 2MHz, 10 = 10MHz) | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*pinID)); // Speed mask | |
// GPIOB->OSPEEDR |= 0x02<<(2*pinID); | |
// | |
// // Set I/O as no pull-up pull-down (00) | |
// GPIOB->PUPDR &= ~(0x03<<(2*pinID)); | |
//} | |
///* Toggle an output pin of Port B */ | |
//void GPIO_PortB_Pin_Toggle(int pinID) { | |
// GPIOB->ODR ^= 1<<pinID; | |
//} | |
///* Init a pin of Port A as digital input, input type of push-pull */ | |
//void GPIO_PortA_Input_Pin_Init(int pinID) { | |
// // Set pin as digital input (00) | |
// GPIOA->MODER &= ~(0x03<<(2*pinID)); | |
// | |
// // Set output type of pin as push-pull (0) | |
// GPIOA->OTYPER &= ~(0x01<<pinID); | |
// | |
// // Set pin's output speed (01 = 2MHz, 10 = 10MHz) | |
// GPIOA->OSPEEDR &= ~(0x03<<(2*pinID)); // Speed mask | |
// GPIOA->OSPEEDR |= 0x02<<(2*pinID); | |
// | |
// // Set I/O as no pull-up pull-down (00) | |
// GPIOA->PUPDR &= ~(0x03<<(2*pinID)); | |
//} | |
///* delay */ | |
//void delay(int t) { | |
// int count = 0; | |
// while (count < t) { | |
// count++; | |
// }; | |
//} | |
////****************************************************************************************** | |
////* The main program starts here: | |
////* Toggle two LEDs when the user button is pressed. | |
////****************************************************************************************** | |
///* LED definitions */ | |
//#define GREEN_LED 7 // GREEN LED: connected to PB7 | |
//#define BLUE_LED 8 // BLUE LED: connected to PB6 | |
////#define BTN0 | |
//int main(void){ | |
// int pinPA0 = 1; | |
// | |
// GPIO_Clock_Enable(); // Enable the clock for ports A&B | |
// | |
// GPIO_PortB_Output_Pin_Init(GREEN_LED); // Init the pins attached to the LEDs as digital outputs | |
// GPIO_PortB_Output_Pin_Init(BLUE_LED); | |
// | |
// GPIO_PortA_Input_Pin_Init(1); // Init the pin PA0 attached to the user button as digital input | |
// | |
// // turn-on BLUE led, turn-off GREEN led | |
// GPIOB->ODR |= 1<<BLUE_LED; | |
// GPIOB->ODR &= ~(1<<GREEN_LED); | |
// | |
// // Loop forever | |
// while(1) { | |
// if (GPIOA->IDR & 0x01) { // User button pressed | |
// while (GPIOA->IDR & 0x01) {} // wait until button released | |
// GPIO_PortB_Pin_Toggle(GREEN_LED); // Toggle the green LED | |
// GPIO_PortB_Pin_Toggle(BLUE_LED); // Toggle the blue LED | |
// } | |
// }; | |
//} | |
//SESSION3 blind led with PWM | |
//******************** (C) Yifeng ZHU ******************** | |
// @file main.c | |
// @author Yifeng Zhu | |
// @version V1.0.0 | |
// @date November-11-2012 | |
// @note | |
// @brief C code for STM32L1xx Discovery Kit | |
// @note | |
// This code is for the book "Embedded Systems with ARM Cortex-M3 | |
// Microcontrollers in Assembly Language and C, Yifeng Zhu, | |
// ISBN-10: 0982692625. | |
// @attension | |
// This code is provided for education purpose. The author shall not be | |
// held liable for any direct, indirect or consequential damages, for any | |
// reason whatever. More information can be found from book website: | |
// http://www.eece.maine.edu/~zhu/book | |
//******************************************************** | |
//#include <stdint.h> | |
/* Standard STM32L1xxx driver headers */ | |
//#include "stm32l1xx.h" | |
//****************************************************************************************** | |
//* The main program starts here: | |
//* Toggle two LEDs every 1ms using SysTick | |
//****************************************************************************************** | |
///* LED definitions */ | |
//#define GREEN_LED 7 // GREEN LED: connected to PB7 | |
//#define BLUE_LED 6 // BLUE LED: connected to PB6 | |
//uint32_t TimingDelay; // time delay specified by user (in number of SysTick interrupts generated) | |
///* STM32L1xx Discovery Kit: | |
// - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK RCC_AHBENR_GPIOAEN | |
// - RESET Pushbutton: connected RESET | |
// - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK RCC_AHBENR_GPIOBEN | |
// - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK RCC_AHBENR_GPIOBEN | |
// - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0, PB1 (group 3) | |
//*/ | |
//// Enable the clock to GPIO Ports A&B | |
//void GPIO_Clock_Enable() { | |
// RCC->AHBENR |= RCC_AHBENR_GPIOAEN; | |
// RCC->AHBENR |= RCC_AHBENR_GPIOBEN; | |
//} | |
///* Init a pin of Port B as digital output, output type of push-pull */ | |
//void GPIO_PortB_Output_Pin_Init(int pinID) { | |
// // Set pin as digital output (01) | |
// GPIOB->MODER &= ~(0x03<<(2*pinID)); | |
// GPIOB->MODER |= 0x01<<(2*pinID); | |
// | |
// // Set output type of pin as push-pull (0) | |
// GPIOB->OTYPER &= ~(0x01<<pinID); | |
// | |
// // Set pin's output speed (01 = 2MHz, 10 = 10MHz) | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*pinID)); // Speed mask | |
// GPIOB->OSPEEDR |= 0x02<<(2*pinID); | |
// | |
// // Set I/O as no pull-up pull-down (00) | |
// GPIOB->PUPDR &= ~(0x03<<(2*pinID)); | |
//} | |
///* Toggle an output pin of Port B */ | |
//void GPIO_PortB_Pin_Toggle(int pinID) { | |
// GPIOB->ODR ^= 1<<pinID; | |
//} | |
///* delay */ | |
//// nTime: number of times of decrement of TimingDelay variable | |
//void Delay(uint32_t nTime) { | |
// TimingDelay = nTime; | |
// while (TimingDelay != 0); | |
//} | |
//// Init SystTick timer to generate interrupts at a fixed-time interval | |
//// input: ticks = number of ticks bw 2 interrupts | |
//void SysTick_Init(uint32_t ticks) { | |
// | |
// // Disable SysTick IRQ & SysTick counter | |
// SysTick->CTRL = 0; | |
// | |
// // Set reload register | |
// SysTick->LOAD = ticks - 1; | |
// | |
// // Set priority | |
// NVIC_SetPriority(SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); | |
// | |
// // Reset SysTick counter value | |
// SysTick->VAL = 0; | |
// | |
// // Select processor clock (set bit CLKSOURCE of SysTick_CTRL reg. to 1) | |
// SysTick->CTRL |= SysTick_CTRL_CLKSOURCE; | |
// | |
// // Enable SysTick IRQ and SysTick timer | |
// SysTick->CTRL |= SysTick_CTRL_ENABLE; | |
// | |
// // Enable SysTick exception request | |
// SysTick->CTRL |= SysTick_CTRL_TICKINT; | |
//} | |
//// SysTick ISR | |
//void SysTick_Handler() { | |
// if (TimingDelay != 0) | |
// TimingDelay--; | |
//} | |
//int main(void){ | |
// RCC->ICSCR |= RCC_ICSCR_MSIRANGE_6; | |
// GPIO_Clock_Enable(); // Enable the clock for ports A&B | |
// | |
// // Init the pins attached to the LEDs as digital outputs | |
// GPIO_PortB_Output_Pin_Init(GREEN_LED); | |
// GPIO_PortB_Output_Pin_Init(BLUE_LED); | |
// | |
// // turn-on BLUE led, turn-off GREEN led | |
// GPIOB->ODR |= 1<<BLUE_LED; | |
// GPIOB->ODR &= ~(1<<GREEN_LED); | |
// SysTick_Init(4194); // Init SystTick timer to generate interrupts at every 1 ms (uP clock at default MSI 2.097 MHz) | |
// while(1) { | |
// //if (mk==1000) mk=3000; | |
// //else mk=1000; | |
// //Delay(mk); // delay 50*1ms | |
// GPIO_PortB_Pin_Toggle(GREEN_LED); // Toggle the green LED | |
// GPIO_PortB_Pin_Toggle(BLUE_LED); // Toggle the blue LED | |
// } | |
// | |
//} | |
//======================= CODE CHUAN ================================ | |
// LAB 1, BLIND LED WITH BUTTON | |
//#include <stdint.h> | |
//#include "stm32l1xx.h" | |
//void GPIO_Clock_Enable(){ //enable the clock to port A,B | |
// RCC->AHBENR |= RCC_AHBENR_GPIOBEN; //port B | |
// RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // port A | |
//} | |
//void LED_BLUE(){ // set pin x I/O mode as GP Output. | |
// GPIOB->MODER &=~(0x03<<(2*6)); // reset state 0x03 trang thai output trang 302 | |
// GPIOB->MODER |=(0x01<<(2*6)); // set pin x as a digital output. 01 | |
// GPIOB->OTYPER &=~(1<<6); // output type push-pull .0 | |
// GPIOB->OSPEEDR &=~(0x03<<(2*6)); // set I/O speed // set mark | |
// GPIOB->OSPEEDR |=(0x01<<(2*6)); // set 01. 2MHz | |
// GPIOB->PUPDR &= ~(0x03<<(2*6)); // set no pull up, no pull down. | |
//} | |
//void LED_GREEN() { | |
// GPIOB->MODER &= ~(0x03<<(2*7)); | |
// GPIOB->MODER |= 0x01<<(2*7); | |
// GPIOB->OTYPER &= ~(1<<7); | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*7)); | |
// GPIOB->OSPEEDR |= 0x01<<(2*7); | |
// GPIOB->PUPDR &= ~(0X03<<(2*7)); | |
//} | |
//void BUTTON_A(){ | |
// GPIOA->MODER &=~(0x03); // set pin 0 I/O mode as GP input. 00 | |
// GPIOA->OTYPER &=~(0x1); //output type pushpull. 0 | |
// GPIOA->OSPEEDR &=~(0x03); //mark | |
// GPIOA->OSPEEDR |= 0x01; //set 01. 2MHz | |
// GPIOA->PUPDR &= ~(0x03); //00. no pull up, no pull down | |
//} | |
//int main(void){ | |
// int delay = 0; | |
// GPIO_Clock_Enable(); //enable port A and B | |
// LED_BLUE(); //config PA6 | |
// LED_GREEN(); //config PA7 | |
// BUTTON_A(); //config PA0 | |
// GPIOB->ODR |=1<<7; //turn off a yellow led | |
// GPIOB->ODR &=~(1<<6); //turn on a blue led | |
// while (1){ | |
// if ((GPIOA->IDR &1) ==1) { // button is pressed | |
// GPIOB->ODR ^=1<<6; //change status of led 6 | |
// GPIOB->ODR ^=1<<7; //change status of led 7 | |
// for (delay = 0; delay < 100000; delay++){} //delay for a range time | |
// } | |
// }; | |
//} | |
// LAB5, BLINK LED(DELAY) WITH SYSTICKTIMER v0.1 | |
//#include <stdint.h> | |
//#include "stm32l1xx.h" | |
//uint32_t TimingDelay; | |
//void GPIOB_Clock_Enable() | |
//{ | |
// RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable the clock to port B | |
//} | |
//void GPIOB_Pin_Init() | |
//{ | |
// // set pin 6 I/O mode as general-purpose output: 01-digital output | |
// GPIOB->MODER &= ~(0x03<<(2*6)); | |
// GPIOB->MODER |= 0x01<<(2*6); // set bit 12 | |
// // set pin 7 I/O mode as general-purpose output: 01-digital output | |
// GPIOB->MODER &= ~(0x03<<(2*7)); | |
// GPIOB->MODER |= 0x01<<(2*7); // set bit 14 | |
// // set output type of pin 6 as push pull: 0-push-pull | |
// GPIOB->OTYPER &= ~(1<<6); | |
// // set output type of pin 7 as push pull: 0-push-pull | |
// GPIOB->OTYPER &= ~(1<<7); | |
// // set I/O output speed | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*6)); | |
// GPIOB->OSPEEDR |= 0x01<<(2*6); | |
// GPIOB->PUPDR &= ~(0x03<<(2*6)); // no pull up pull down | |
// | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*7)); | |
// GPIOB->OSPEEDR |= 0x01<<(2*7); | |
// GPIOB->PUPDR &= ~(0x03<<(2*7)); | |
//} | |
//void SysTick_Initialize(uint32_t ticks) | |
//{ | |
//// set MSIRANGE= 110 --> 4.194MHz multispeed | |
// //define tan so dong 2601 stm32l1xx.h | |
// //tham khao Reference Manual 6.3.2 pg.141 | |
// /* bits [13:15] cua thanh ghi RCC_ICSCR row.513 stm32l1xx.h | |
// 000: range 0 around 65.536 kHz | |
// 001: range 1 around 131.072 kHz | |
// 010: range 2 around 262.144 kHz | |
// 011: range 3 around 524.288 kHz | |
// 100: range 4 around 1.048 MHz | |
// 101: range 5 around 2.097 MHz (reset value) | |
// 110: range 6 around 4.194 MHz | |
// 111: not allowed | |
// */ | |
// /* | |
// * Ngoai ra con HSIRANGE Highspeed 16Mhz | |
// HSERANGE su dung bo dem ngoai (external osillator) | |
// Tuy chon Ranges re-Define RCC_CFGR[1:0] define in r.2629 of stm32l1.h; pg.263 reference manual stm32l1 | |
// reference: http://athousandprojects.com/stm32/stm32l152-clock-overview/ | |
// http://athousandprojects.com/stm32/stm32l152-clock-code-examples/ | |
// pg. 128 reference manual stm32l1 | |
// */ | |
// RCC->ICSCR &= ~(7 << 13); | |
// RCC->ICSCR |= (6 << 13); //110..[13bits] :range 6 (4.194) MHz | |
//// Disable Systick IRQ and Systick counter | |
// SysTick->CTRL = 0; | |
// | |
// // Set reload register | |
// SysTick->LOAD = ticks - 1; | |
// | |
// //Set priority | |
// //NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS)-1); | |
// | |
// // Reset the SysTick counter value | |
// SysTick->VAL = 0; | |
// | |
// //Select processor clock | |
// //1 = processor clock; 0 = external clock | |
// SysTick->CTRL |= SysTick_CTRL_CLKSOURCE;//0x04 | |
// | |
// //Enable SysTick IRQ and SysTick Timer | |
// SysTick->CTRL |= SysTick_CTRL_ENABLE;// 0x01 | |
// | |
// // Enables SysTick exception request | |
// // 1 = counting down zero asserts the SysTick exception request | |
// // 2 = counting down zero do not asserts the SysTick exception request | |
// SysTick->CTRL |= SysTick_CTRL_TICKINT;//0x02 | |
//} | |
//void SysTick_Handler(void) // SysTick interrupt service routine | |
//{ | |
// if(TimingDelay !=0) // prevent it from being negative | |
// TimingDelay--; | |
//} | |
//void Delay(uint32_t nTime) | |
//{ | |
// // nTime : specifies the delay time length | |
// TimingDelay = nTime; | |
// while(TimingDelay!=0); | |
//} | |
//void Toggle(void){ | |
// GPIOB->ODR ^= 1<<6; | |
// GPIOB->ODR ^= 1<<7; | |
//} | |
//int main(void){ | |
// GPIOB_Clock_Enable(); | |
// GPIOB_Pin_Init(); | |
// SysTick_Initialize(4194); // 4194 xung nhip | |
// GPIOB->ODR &= ~(1<<6); // set pb 6 low | |
// GPIOB->ODR |= 1<<7; // set pb 7 high | |
// while(1){ | |
// Delay(1000); | |
// Toggle(); | |
// } | |
// | |
//} | |
// LAB5, BLINK LED(DELAY) WITH SYSTICKTIMER v0.2 | |
#include <stdint.h> | |
#include "stm32l1xx.h" | |
uint32_t TimingDelay; | |
void GPIOB_Clock_Enable() | |
{ | |
RCC->AHBENR |= 0x0000002; // enable the clock to port B | |
} | |
void GPIOB_Pin_Init() | |
{ | |
// set pin 6 I/O mode as general-purpose output: 01-digital output | |
GPIOB->MODER &= ~(0x03<<(2*6)); | |
GPIOB->MODER |= 0x01<<(2*6); // set bit 12 | |
// set pin 7 I/O mode as general-purpose output: 01-digital output | |
GPIOB->MODER &= ~(0x03<<(2*7)); | |
GPIOB->MODER |= 0x01<<(2*7); // set bit 14 | |
// set output type of pin 6 as push pull: 0-push-pull | |
GPIOB->OTYPER &= ~(1<<6); | |
// set output type of pin 7 as push pull: 0-push-pull | |
GPIOB->OTYPER &= ~(1<<7); | |
// set I/O output speed | |
GPIOB->OSPEEDR &= ~(0x03<<(2*6)); | |
GPIOB->OSPEEDR |= 0x01<<(2*6); | |
// set I/O as no pull-up, no pull-down: 00 | |
GPIOB->PUPDR &= ~(0x03<<(2*6)); | |
// set I/O output speed | |
GPIOB->OSPEEDR &= ~(0x03<<(2*7)); | |
GPIOB->OSPEEDR |= 0x01<<(2*7); | |
// set I/O as no pull-up, no pull-down: 00 | |
GPIOB->PUPDR &= ~(0x03<<(2*7)); | |
} | |
void SysTick_Initialize(uint32_t ticks) | |
{ | |
// set MSIRANGE= 110 --> 4.194MHz | |
RCC->ICSCR &= ~(7 << 13); | |
RCC->ICSCR |= (6 << 13); | |
// Disable Systick IRQ and Systick counter | |
SysTick->CTRL = 0; | |
// Set reload register | |
SysTick->LOAD = ticks - 1; | |
//Set priority | |
//NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS)-1); | |
// Reset the SysTick counter value | |
SysTick->VAL = 0; | |
//Select processor clock | |
//1 = processor clock; 0 = external clock | |
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE;//0x04 | |
//Enable SysTick IRQ and SysTick Timer | |
SysTick->CTRL |= SysTick_CTRL_ENABLE;// 0x01 | |
// Enables SysTick exception request | |
// 1 = counting down zero asserts the SysTick exception request | |
// 2 = counting down zero do not asserts the SysTick exception request | |
SysTick->CTRL |= SysTick_CTRL_TICKINT;//0x02 | |
} | |
void Toggle(void){ | |
GPIOB->ODR ^= 1<<6; | |
GPIOB->ODR ^= 1<<7; | |
} | |
void SysTick_Handler(void) // SysTick interrupt service routine | |
{ | |
Toggle(); | |
} | |
int main(void){ | |
GPIOB_Clock_Enable(); | |
GPIOB_Pin_Init(); | |
GPIOB->ODR &= ~(1<<6); // set pb 6 low | |
GPIOB->ODR |= 1<<7; // set pb 7 high | |
SysTick_Initialize(4194000); // 4194000 xung nhip <=> 1s | |
while(1){ | |
} | |
} | |
// LAB6 THUC HANH BAM XUNG (KHUYEN MAI OF QUY) | |
//#include <stdint.h> | |
//#include "stm32l1xx.h" | |
//uint32_t TimingDelay; | |
//void TIM4_IRQHandler(void){ | |
//} | |
////Enable the clock port B | |
//void GPIOB_Clock_Enable() | |
//{ | |
// RCC->AHBENR |= 0x0000002; | |
//} | |
////Enable the clock of timer 4 | |
//void GPIO_Timer4_Clock_Enable(){ | |
// RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; | |
//} | |
////Configure GPIO pins | |
//void GPIOB_Pin6_Init() | |
//{ | |
// // set pin 6 I/O mode as general-purpose output: 10 -alternative function | |
// GPIOB->MODER &= ~(0x03<<(2*6)); | |
// GPIOB->MODER |= 0x02<<(2*6); // set bit 13 | |
// // Pin PB 6 as alternative function 2 (TIM4) | |
// GPIOB->AFR[0] &=~(0xF<<(4*6)); | |
// GPIOB->AFR[0] |=0x02<<(4*6); | |
// // Set I/O output speed value as 40MHz | |
// // 400KHz(00), 2MHz(01), 10MHz(10), 40MHz(11) | |
// GPIOB->OSPEEDR &= ~(0x03<<(2*6)); | |
// GPIOB->OSPEEDR |= 0x03<<(2*6); | |
// // set I/O as no pull-up, no pull-down: 00 | |
// GPIOB->PUPDR &= ~(0x03<<(2*6)); | |
// // set output type of pin 6 as push pull: 0-push-pull | |
// GPIOB->OTYPER &=~(1<<6); | |
//} | |
//void TIM4_Init(){ | |
// //Set the prescaler value | |
// TIM4->PSC = 2097000/1000-1; | |
// //Set the auto-reload value: upcounting (0->ARR), downcounting (ARR->0) | |
// TIM4->ARR = 1000; | |
// | |
// TIM4->CCR1 = 500; | |
// | |
// //Set PWM mode 1 or mode 2 on chanel 1, channel 2 | |
// TIM4->CCMR1 |= TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1; //bit 1 0X020 | bit 2 0x040 | |
// | |
// TIM4->CCER |= TIM_CCER_CC1E; // Enable output for channel 1- 0x001 | |
// | |
// TIM4->CR1 |= TIM_CR1_CEN; // ENABLE counter - 0x001 | |
//} | |
//int main(void){ | |
// int i; | |
// int brightness = 1; | |
// int brightness_1=199; | |
// int stepSize = 1; | |
// GPIOB_Clock_Enable(); | |
// GPIO_Timer4_Clock_Enable(); | |
// GPIOB_Pin6_Init(); | |
// TIM4_Init(); | |
// while(1){ | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment