We need to generate PWM signal on PB14(embeded with an LED D1, or D2 if you like)
> when the `PB14` output `LOW`, `D1` will light up.
In STM32, Input Capture and Output Compare is configured in Timer channels.
Let’s check which Timer is connected to PB14
It's the `CH2N` channel of timer 1.
Then we configure timer 1.
Prescaler: 7200-1
Counter Period: 200-1 (50Hz)
Pulse: 180 (duty cycle of 90%; TIM_CNT 0~179: HIGH, TIM_CNT 180~199: LOW)
CHN Polarity: LOW (needed for CHN)
> CHxN is the complementary channel of CHx in Timer1.
> If the polarity of CHxN and CHx are different, the two channels will follow the same output pattern, otherwise they will be complementary
Also remember to configure PB12 (SW4) as GPIO_Input.
> You can try to use Input Capture to capture the press action of the button (as indicated in lab manual, i.e. connecting `PB12` to `PA0`).
## Code
Init PWM in `main()`:
1 2 3 4
/* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); /* USER CODE END 2 */
Get the press/release action of the button (PB12):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int pressed = 0; /* USER CODE BEGIN WHILE */ while (1) { // main loop /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if (!HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) && pressed == 0) { pressed = 1; // what to do when pressed } elseif (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) && pressed == 1) { pressed = 0; // what to do when releassed } } /* USER CODE END 3 */
Posted Updated Notea few seconds read (About 75 words)
VDD (Pin 2): Power supply (usually +5V). Note: If you use J-link debugger to power the board, the output voltage at +5V pin will not be 5V, maybe around 2.8V, so remember to use the 5V power supply wire to power the board.
VO (Pin 3): Contrast adjustment pin. Usually connected to a potentiometer to adjust the contrast.
RS (Pin 4): Register select pin. Low for instruction register, high for data register.
RW (Pin 5): Read/Write pin. Low for write operation, high for read operation. Usually grounded (write mode).
E (Pin 6): Enable pin. The LCD controller only captures (grabs) the data presented at its register lines(D0-D7) only when the E pin “transitions” from high to low.
D0-D7 (Pins 7-14): Data bus pins. Used for communication with the microcontroller in either 4-bit (D4-D7) or 8-bit (D0-D7) mode.
A (Pin 15): For the backlight. Typically connected to +5V.
K (Pin 16): For the backlight. Typically grounded (GND).
voidLCD_init(void) { LCD_Write_Command(LCD_2_LINE_8_BITS); // function set -8 bit interface Delay_ms(5); LCD_Write_Command(LCD_2_LINE_8_BITS); LCD_Write_Command(LCD_CLR_DSP); // clear display Delay_us(100); LCD_Write_Command(LCD_CSR_INC); // Set entry mode: increment Delay_us(100); LCD_Write_Command(LCD_DSP_CSR); // open display, close cursor }
Display Charactor
First we need to set the cursor position
The cursor position is basically the DDRAM address that you want to write data in, which is corresponding to the display location on LCD screen.
1 2 3 4 5 6 7 8 9
voidLCD_Set_Position(uchar x, uchar y) { if (y == 0) // first line { LCD_Write_Command(0x80 + x); } elseif (y == 1) // second line { LCD_Write_Command(0xc0 + x); } }
If we want to display a character at first line, first column:
1
LCD_Set_Position(0,0);
Then, we need to write data into DDRAM[addr] to tell the LCD which character you want to display, for example “J”.