Set 3

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#include "stm32g0xx.

h"

void msdelay(uint32_t value){


long int loops = 850 * value * 4;
while (loops--) {}
}

int main(void) {

RCC->IOPENR |= 0x00000005;// Enable peripheral clock , bit 0 of A AND bit 2 of


C
GPIOA->MODER &= 0xFFFFF3FF;// Configure PA5 (LED4) as output
GPIOA->MODER |= 0x00000400;
GPIOC->MODER &= 0xF3FFFFFF;// Configure PC13 (User Button) as input
while (1) {
while (GPIOC->IDR & 0x00002000);// Wait for user to press BUTTON
for (int i = 0; i < 3; i++) {// Toggle LED at 2Hz for 3 times
GPIOA->BSRR=0x00000020; // SETS BIT 5
msdelay(500); // (2Hz)
GPIOA->BSRR=0x00200000;// CLEARS BIT 21
msdelay(500); // (2Hz)
}
while (GPIOC->IDR & 0x00002000);// Wait for user to press BUTTON
for (int i = 0; i < 3; i++) { // Toggle LED at 1Hz for 3 times
GPIOA->BSRR=0x00000020;// SETS BIT 5
msdelay(1000); //(1Hz)
GPIOA->BSRR=0x00200000;// CLEARS BIT 21
msdelay(1000); //(1Hz)
}
}
return 0;
}
Pr set 3 part B

#include "stm32g071xx.h" // Include header file for STM32G071 microcontroller


#define GPIO_Pin_5 ((uint16_t)0x0020) // Define GPIO pin 5

void delay_ms(uint32_t milliseconds){ // Function to create a delay in


milliseconds
uint32_t i; // Declare loop counter variable
for(i=0;i<(milliseconds*1000);i++){ // Loop to create delay
__NOP(); // No operation (assembly instruction to waste cycles)
}
}

void init_GPIO(void){ // Function to initialize GPIO


RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // Enable GPIOA clock
GPIOA->MODER &= -(GPIO_MODER_MODE5_Msk); // Clear bits for pin 5 in GPIOA
mode register
GPIOA->MODER |= GPIO_MODER_MODE5_0; // Set pin 5 to output mode
}

void TOGGLE_LED(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin){ // Function to toggle


LED
GPIOA->ODR ^= GPIO_Pin; // Toggle pin specified by GPIO_Pin parameter
}
int main(void){ // Main function
init_GPIO(); // Initialize GPIO
while(1){ // Infinite loop
for(int i=0; i<3; i++){ // Loop three times
TOGGLE_LED(GPIOA, GPIO_Pin_5); // Toggle LED
delay_ms(500); // Delay 500 milliseconds
}

delay_ms(5000); // Delay 5000 milliseconds

for(int i=0; i<3; i++){ // Loop three times


TOGGLE_LED(GPIOA, GPIO_Pin_5); // Toggle LED
delay_ms(250); // Delay 250 milliseconds
}
}
}
Pr set 3 part A

#include "stm32g071xx.h"
#include "math.h"

void msdelay (uint32_t value);


uint8_t keypad_scan (void);
uint8_t keyval, keyval1;

int main(void)
{
uint8_t rows, cols, j, k;
// Enable clock for Port A and C
RCC->IOPENR |=RCC_IOPENR_GPIOAEN;
RCC->IOPENR |=RCC_IOPENR_GPIOCEN;
// Set pin 4 to 7 of port C as columns (output:pull-push)
GPIOC->MODER &= 0xFFFF00FF; // clear required field
GPIOC->MODER |= 0X00005500; // set required bits
GPIOC->OTYPER &= 0xFFFFFF0F; // use pull-push type
GPIOC->PUPDR &= 0xFFFF00FF; // no pull up, no pull down

// Set pins 8 to 11 of port A as rows (mode: input)


GPIOA->MODER &= 0xFF00FFFF; // Pin 8-11 input mode
GPIOA->PUPDR &= 0xFF00FFFF; // with resistive pull down
GPIOA->PUPDR |= 0x00AA0000;

// Set Port A bit 5 as output mode (pull-push)


GPIOA->MODER &= 0xFFFFF3FF; // clear required field
GPIOA->MODER |= 0x00000400; // set required bits

while(1){
GPIOC->ODR |= 0x00F0; // all columns HIGH
keyval = keypad_scan();
keyval1 = keyval;
cols = keyval & 0x0F;
cols = cols/2;
if (cols==4) cols-=1;
keyval = keyval >>4;
rows = keyval & 0x0F;
rows = rows/2;
if(rows==4) rows -=1;
for (j=0; j<=rows; ++j) {
GPIOA->BSRR = (1<<5); msdelay (250);
GPIOA->BSRR = (1<<21); msdelay (250);
}
msdelay(2000);
for (k=0; k<=cols; ++k) {
GPIOA->BSRR = (1<<5); msdelay (250);
GPIOA->BSRR = (1<<21); msdelay (250);
}
}
} // end of main

uint8_t keypad_scan (void) // function


{
uint32_t rowval =0;
uint8_t c,j=0;
uint8_t keycode =0;

rowval = GPIOA->IDR & 0x00000F00;


while (rowval !=0){ // loop if key remains closed
rowval = GPIOA->IDR & 0x00000F00;
}

while(1){
rowval = GPIOA->IDR;
if (rowval!=0){
msdelay(10); // debounce delay
rowval = GPIOA->IDR & 0x00000F00;
if (rowval !=0)
break; // key press detected, go to scan loop
else continue;
}
else
continue;
}

for(j=0; j<4; j++) { // scan cycle loop


c = pow (2,j);
if (c==1) {
GPIOC->BSRR = 0x00E00010; // set PC4 and reset PC5,PC6 & PC7
msdelay(1);
rowval = GPIOA->IDR & 0x00000F00;
if (rowval !=0) break; }
else if (c==2) {
GPIOC->BSRR = 0x00D00020; // set PC5 and reset PC4,PC6 & PC7
msdelay(1);
rowval = GPIOA->IDR & 0x00000F00;
if (rowval !=0) break; }
if (c==4) {
GPIOC->BSRR = 0x00B00040; // set PC6 and reset PC4,PC5 & PC7
msdelay(1);
rowval = GPIOA->IDR & 0x00000F00;
if (rowval !=0) break; }
if (c==8) {
GPIOC->BSRR = 0x00700080; // set PC7 and reset PC4,PC5 & PC6
msdelay(1);
rowval = GPIOA->IDR & 0x00000F00;
if (rowval !=0) break; }
} //end of for loop
rowval = rowval >>4;
keycode = rowval | c; //combine row & column index
return keycode;
}

void msdelay (uint32_t value) //function


{
long int loops;
loops = 850*value*4;
while(loops--){}
}
Pr set 3 part D Keypad

You might also like