按2次增加一个num:
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure0;
GPIO_InitStructure0.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure0.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure0.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure0);
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure1);
OLED_Init();
uint8_t num = 0;
uint8_t con = 0;
OLED_ShowString(1,1,"Num:");
OLED_ShowNum(2,1,num,5);
while(1){
uint8_t key = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0); /* 按键读取,存的是过去的值 */
if(key == 0){
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0);/* 要求现取,在检查本身,按下去的状态会一直卡在while循环里,放开才会跳出循环继续往下 */
Delay_ms(20);
if(con%2 == 0){
num += 1;
}
con+=1;
OLED_ShowNum(2,1,num,5);
uint8_t led = GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_5);
GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)(1-led));
}
}
}
按一下oled数字+=1,灯亮/灭:
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure0;
GPIO_InitStructure0.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure0.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure0.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure0);
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure1);
OLED_Init();
uint8_t num = 0;
OLED_ShowString(1,1,"Num:");
OLED_ShowNum(2,1,num,5);
while(1){
uint8_t key = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0); /* 按键读取,存的是过去的值 */
if(key == 0){
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0);/* 要求现取,在检查本身,按下去的状态会一直卡在while循环里,放开才会跳出循环继续往下 */
Delay_ms(20);
num+=1;
OLED_ShowNum(2,1,num,5);
uint8_t led = GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_5);
GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)!led);
}
}
}
按键+光敏+唱歌:
#include "stm32f10x.h"
#include "Delay.h"
#define DO 262
#define RE 294
#define MI 330
#define FA 349
#define SOL 392
#define LA 440
#define SI 494
#define SOL_L 196
#define DO_H 523
void Beep(uint16_t freq, uint16_t ms)
{
uint32_t half_us = 500000 / freq;
uint32_t cycles = (uint32_t)freq * ms / 1000;
for (uint32_t i = 0; i < cycles; i++)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
Delay_us(half_us);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
Delay_us(half_us);
}
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure0;
GPIO_InitStructure0.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure0.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure0.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure0);
Beep(DO, 400); Beep(RE, 400); Beep(MI, 400); Beep(FA, 400);
Beep(SOL, 400); Beep(LA, 400); Beep(SI, 400);
uint16_t tune[] = {
SOL_L, DO, /* : 5, 1 */
RE, RE, FA, SOL, SOL, DO_H, /* 2 2 4 5 5 1' */
SOL, SOL, FA, RE, SOL, SOL, /* 5 5 4 2 5 5 */
RE, SOL, SOL, RE, DO, /* 2 5 5 2 1 */
SOL_L, SOL_L, DO, /* 5,- 5, 1 */
RE, RE, FA, SOL, SOL, DO_H,
SOL, SOL, FA, RE, SOL, SOL,
RE, SOL, SOL, RE, DO,
RE, SOL, DO_H /* 2 - 5 1' */
};
uint16_t beat[] = {
400, 400,
400, 200, 200, 400, 200, 200,
400, 200, 200, 400, 200, 200,
400, 200, 200, 400, 400,
800, 200, 200,
400, 200, 200, 400, 200, 200,
400, 200, 200, 400, 200, 200,
400, 200, 200, 400, 400,
1200, 200, 200
};
uint8_t len = sizeof(tune) / sizeof(tune[0]);
uint8_t light_last = 1;
while (1) {
uint8_t light = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
GPIO_WriteBit(GPIOA, GPIO_Pin_5, (BitAction)light);
if (light_last == 1 && light == 0) {
for (int i = 0; i < len; i++) {
Beep(tune[i], beat[i]);
Delay_ms(60);
}
light = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
}
light_last = light;
}
}
按键蜂鸣
#include "stm32f10x.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitTypeDef GPIO_InitStructure0;
GPIO_InitStructure0.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure0.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure0.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure0);
//Beep(DO, 400); Beep(RE, 400); Beep(MI, 400); Beep(FA, 400);
//Beep(SOL, 400); Beep(LA, 400); Beep(SI, 400);
//uint16_t tune[] = {DO, DO, SOL, SOL, LA, LA, SOL, FA, FA, MI, MI, RE, RE, DO};
//uint16_t beat[] = {400,400,400,400,400,400,800, 400,400,400,400,400,400,800};
//for (int i = 0; i < 14; i++){
// Beep(tune[i], beat[i]);
// Delay_us(100);
//}
while (1) {
uint8_t key = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)key);
}
}
点灯PC13
#include "stm32f10x.h"
void Delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 7200; j++);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
while (1)
{
}
}
闪灯PC13
#include "stm32f10x.h"
void Delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 7200; j++);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
while (1)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13);
Delay_ms(120);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
Delay_ms(120);
}
}
流水灯
#include "stm32f10x.h"
void Delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 7200; j++);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure1;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure1);
GPIO_InitTypeDef GPIO_InitStructure2;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure2);
GPIO_InitTypeDef GPIO_InitStructure3;
GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure3.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure3);
while (1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_0);
Delay_ms(120);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(120);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
Delay_ms(120);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
Delay_ms(120);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
Delay_ms(120);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
Delay_ms(120);
}
}
蜂鸣器唱小星星:
#include "stm32f10x.h"
#define DO 262
#define RE 294
#define MI 330
#define FA 349
#define SOL 392
#define LA 440
#define SI 494
void Delay_us(uint32_t us)
{
uint32_t i, j;
for (i = 0; i < us; i++)
for (j = 0; j < 7; j++);
}
void Beep(uint16_t freq, uint16_t ms)
{
uint32_t half_us = 500000 / freq;
uint32_t cycles = (uint32_t)freq * ms / 1000;
for (uint32_t i = 0; i < cycles; i++)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
Delay_us(half_us);
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
Delay_us(half_us);
}
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
Beep(DO, 400); Beep(RE, 400); Beep(MI, 400); Beep(FA, 400);
Beep(SOL, 400); Beep(LA, 400); Beep(SI, 400);
uint16_t tune[] = {DO, DO, SOL, SOL, LA, LA, SOL, FA, FA, MI, MI, RE, RE, DO};
uint16_t beat[] = {400,400,400,400,400,400,800, 400,400,400,400,400,400,800};
for (int i = 0; i < 14; i++){
Beep(tune[i], beat[i]);
Delay_us(100);
}
while (1) { }
}
浙公网安备 33010602011771号