gpio,spi,usart, can przyklad


/* Author: Szymon Panecki @ Embedded Systems Scientific Club
Title: Example 7: GPIO(LEDy, przyciski) + SPI(LCD) + USART(RS-232) + CAN
Date: 01.06.2009
*/
#include "main.h"
#include "LCD.h"

GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
USART_InitTypeDef USART_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
u32 i = 0;
CanTxMsg TxMessage;
CanRxMsg RxMessage;
CanRxMsg RxMessage2;
u8 TransmitMailbox;
u16 data = 0;
u8 ReadValue = 3;

void main (void)
{
#ifdef DEBUG
debug();
#endif

RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div2);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN | RCC_APB1Periph_SPI2, ENABLE);

//USART
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//USART
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//CAN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//CAN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//LCD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//LCD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//LCD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);

//PRZYCISKI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

//LEDY
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//LEDY C.D.
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);

//BUZZER
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);

USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);

CAN_DeInit();
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(&CAN_InitStructure);

CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

RxMessage.StdId=0x00;
RxMessage.IDE=CAN_ID_STD;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;

/*
//wyswietlacz
LCD_Init();
LCD_PutString(1,1,"WYSWIETLACZ");
*/

//usart wysylanie
//USART_SendData(USART1,99);

/*
//can wysylanie
TxMessage.StdId=0x04;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.IDE=CAN_ID_STD;
TxMessage.DLC=2;
TxMessage.Data[0]=0xCA;
TxMessage.Data[1]=0xFE;

TransmitMailbox=CAN_Transmit(&TxMessage);
i = 0;
while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF))
{
i++;
}
*/

while(1)
{
/*
//can odbieranie
while (CAN_MessagePending(CAN_FIFO0))
{
CAN_Receive(CAN_FIFO0, &RxMessage);

}

while (CAN_MessagePending(CAN_FIFO1))
{
CAN_Receive(CAN_FIFO1, &RxMessage);
}
*/

/*
//usart odbieranie
if((USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET))
{
data = USART_ReceiveData(USART1);
}
*/

/*
//przyciski
ReadValue = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);
if (ReadValue==0)
{

}

ReadValue = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1);
if (ReadValue==0)
{

}

ReadValue = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3);
if (ReadValue==0)
{

}

ReadValue = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5);
if (ReadValue==0)
{

}
*/

/*
//ledy i buzzer
GPIO_WriteBit(GPIOC, GPIO_Pin_4, Bit_RESET);
GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET);
for(Delay = 0; Delay < 0x5FFF; Delay++) {}
GPIO_WriteBit(GPIOC, GPIO_Pin_4, Bit_SET);
GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);
GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_RESET);
for(Delay = 0; Delay < 0x5FFF; Delay++) {}
*/

}
}
// end of file

Wyszukiwarka

Podobne podstrony:
can przyklad2
can przyklad1
spi przyklad2 update
spi przyklad2
gpio przyklad
Sterowanie rejestrami przesuwnymi z wykorzystaniem interfejsu SPI na przykładzie 6 cyfrowego wyświet
spi przyklad1
gpio przyklad1
Mikrokontrolery STM32 Użycie interfejsu I2C, USART, SPI
usart przyklad
cw6 arkusz obliczeniowy przyklad
przykładowy test A
przykladowyJrkusz150UM[1] drukow
function ncurses can change color
OEiM AiR Przykladowy Egzamin
Znaczenie korytarzy ekologicznych dla funkcjonowania obszarów chronionych na przykładzie Gorców

więcej podobnych podstron