Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file Project/STM32F10x_StdPeriph_Template/main.c
- * @author MCD Application Team
- * @version V3.5.0
- * @date 08-April-2011
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include <stdio.h>
- /* Private typedef -----------------------------------------------------------*/
- typedef struct{
- uint16_t pin; // assign something like GPIO_Pin_8
- GPIO_TypeDef * port; // assign someting like GPIOA
- } MyPin;
- MyPin DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7,DB[8],RS,RW,EN,LED;
- /* Private define ------------------------------------------------------------*/
- /*
- */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Private function prototypes -----------------------------------------------*/
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- /* Private functions ---------------------------------------------------------*/
- void enable_gpio();
- void writePin(MyPin pin, BitAction value);
- BitAction readPin(MyPin pin);
- /*----------------- LCD functions ------------*/
- //Common LCD functions
- void lcdCommon_sendByte(int byte,int howManyPins);
- //8-bit mode LCD functions; you may delete ALL these 5 functions if you are using 4-bit
- void lcd8_init();
- void lcd8_sendCommand(int byte);
- void lcd8_sendData(int byte);
- void lcd8_print( char *s);
- void lcd8_printx(char *s, int line, int column);
- //4-bit mode LCD functions; you may delete ALL these 5 functions if you are using 8-bit
- void lcd4_init();
- void lcd4_sendCommand(int byte);
- void lcd4_sendData(int byte);
- void lcd4_print( char *s);
- void lcd4_printx(char *s, int line, int column);
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- void delay_ms(int ms){
- while(ms--)
- for(volatile uint16_t i = 0; i < 2300; i++);
- }
- void delay_us(volatile int ms){
- ms *= 4;
- while(ms--);
- }
- int main(void)
- {
- enable_gpio();
- /* //8-bit example
- lcd8_init();
- lcd8_printx("8 Bit",0,8);
- lcd8_printx("LCD Driver",1,6);
- lcd8_printx("->",0,0);
- lcd8_printx("aemre",3,15);//
- /*/ // 4-bit example
- lcd4_init();
- lcd4_printx("4 Bit",0,8);
- lcd4_printx("LCD Driver",1,6);
- lcd4_printx("aemre",3,15);// */
- while(1);
- }
- /**
- * bit0 = PA8#2
- * bit1 = PA9#4
- * bit2 = PC10#8
- * bit3 = PB5#12
- * bit4 = PB8#16
- * bit5 = PC1#20
- * bit6 = PC13#24
- * bit7 = PB1#26
- *
- * RS = PB9#17
- * RW = PC0#19
- * EN = PB0#21
- * LED = PC12#10
- */
- void enable_gpio(){
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
- RCC_APB2Periph_GPIOC, ENABLE);
- /* enable lcdCommon_sendByte() function pins */
- GPIO_InitTypeDef GPIO_InitStructureA, GPIO_InitStructureB,GPIO_InitStructureC;
- GPIO_InitStructureA.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_InitStructureB.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_1 | GPIO_Pin_9 | GPIO_Pin_0;
- GPIO_InitStructureC.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_1 | GPIO_Pin_13 | GPIO_Pin_12 | GPIO_Pin_0;
- GPIO_InitStructureA.GPIO_Speed = GPIO_InitStructureB.GPIO_Speed = GPIO_InitStructureC.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructureA.GPIO_Mode = GPIO_InitStructureB.GPIO_Mode = GPIO_InitStructureC.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructureA);
- GPIO_Init(GPIOB, &GPIO_InitStructureB);
- GPIO_Init(GPIOC, &GPIO_InitStructureC);
- DB0.port = GPIOA; DB0.pin = GPIO_Pin_8;
- DB1.port = GPIOA; DB1.pin = GPIO_Pin_9;
- DB2.port = GPIOC; DB2.pin = GPIO_Pin_10;
- DB3.port = GPIOB; DB3.pin = GPIO_Pin_5;
- DB4.port = GPIOB; DB4.pin = GPIO_Pin_8;
- DB5.port = GPIOC; DB5.pin = GPIO_Pin_1;
- DB6.port = GPIOC; DB6.pin = GPIO_Pin_13;
- DB7.port = GPIOB; DB7.pin = GPIO_Pin_1;
- RS.port = GPIOB; RS.pin = GPIO_Pin_9;
- RW.port = GPIOC; RW.pin = GPIO_Pin_0;
- EN.port = GPIOB; EN.pin = GPIO_Pin_0;
- LED.port = GPIOC; LED.pin = GPIO_Pin_12;
- DB[0]=DB0;DB[1]=DB1;DB[2]=DB2;DB[3]=DB3;DB[4]=DB4;DB[5]=DB5;DB[6]=DB6;DB[7]=DB7;
- }
- void lcd8_init(){
- delay_ms(42);
- lcd8_sendCommand(0x3C);// 8-bit interface, 4 lines, 5x8
- delay_us(48);
- lcd8_sendCommand(0x3C);// 8-bit interface, 8 lines, 5x8 - again
- delay_us(48);
- lcd8_sendCommand(0x0C);// display ON, cursor off, blink off
- delay_us(48);
- lcd8_sendCommand(0x01);// clear display
- delay_ms(2);
- lcd8_sendCommand(0x06);// increment cursor, no shift
- }
- void lcd4_init(){
- delay_ms(42);
- /* only most significant 4 bytes sent, lcd4_sendCommand is not used*/
- writePin(RS,Bit_RESET);
- lcdCommon_sendByte(0x30,4);
- /* only most significant 4 bytes sent, lcd4_sendCommand is not used */
- delay_us(48);
- lcd4_sendCommand(0x2C);// 4-bit interface, 8 lines, 5x8 - again
- delay_us(48);
- lcd4_sendCommand(0x0C);// display ON, cursor off, blink off
- delay_us(48);
- lcd4_sendCommand(0x01);// clear display
- delay_ms(2);
- lcd4_sendCommand(0x06);// increment cursor, no shift
- }
- /**
- * Convert boolean to BitAction
- */
- BitAction conv(int b){
- return b ? Bit_SET : Bit_RESET;
- }
- void writePin(MyPin pin, BitAction value){
- GPIO_WriteBit(pin.port,pin.pin,value);
- }
- BitAction readPin(MyPin pin){
- return conv(GPIO_ReadInputDataBit(pin.port, pin.pin));
- }
- void lcdCommon_sendByte(int byte,int howManyPins){
- //First check if LCD is busy
- writePin(EN,Bit_SET);// To send data we need high to low signal
- for(int i = 0; i<howManyPins; i++)
- writePin(DB[7-i],conv(byte & (0x01 << (7-i))));//writePin(DB[7-i],conv(byte & (int) pow(2,7-i)));
- writePin(RW,Bit_RESET); // LCD is now in write mode
- delay_us(100); //wait sometime
- writePin(EN,Bit_RESET); // Now we send high to low signal
- delay_us(50); //it may need some time for the next operation
- }
- void lcd8_sendCommand(int byte){
- writePin(RS,Bit_RESET);
- lcdCommon_sendByte(byte,8);
- }
- void lcd4_sendCommand(int byte){
- writePin(RS,Bit_RESET);
- lcdCommon_sendByte(byte,4);
- lcdCommon_sendByte(byte<<4,4);
- }
- void lcd8_sendData(int byte){
- writePin(RS,Bit_SET);
- lcdCommon_sendByte(byte,8);
- }
- void lcd4_sendData(int byte){
- writePin(RS,Bit_SET);
- lcdCommon_sendByte(byte,4);
- lcdCommon_sendByte(byte<<4,4);
- }
- void lcd8_print( char *s){
- while( *s) lcd8_sendData( *s++);
- }
- void lcd4_print( char *s){
- while( *s) lcd4_sendData( *s++);
- }
- void lcd8_printx( char *s, int line, int column){
- int lineStarts[4] = {128, 192, 148, 212};
- lcd8_sendCommand(lineStarts[line%4]+column%20); //go to given position
- while( *s) lcd8_sendData( *s++);
- }
- void lcd4_printx( char *s, int line, int column){
- int lineStarts[4] = {128, 192, 148, 212};
- lcd4_sendCommand(lineStarts[line%4]+column%20); //go to given position
- while( *s) lcd4_sendData( *s++);
- }
- #ifdef USE_FULL_ASSERT
- void assert_failed(uint8_t* file, uint32_t line)
- {
- while (1);
- }
- #endif
- /**
- * @}
- */
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement