test4

Dependencies:   mbed BufferedSerial LS7366LIB2 FastPWM

Committer:
lsh3146
Date:
Tue Dec 08 01:25:06 2020 +0000
Revision:
4:bf278ddb8504
Parent:
0:7cff999a7f5c
aaaaaqqqqq

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gohgwaja 0:7cff999a7f5c 1 #ifndef _MCP23S17_H_
gohgwaja 0:7cff999a7f5c 2 #define _MCP23S17_H_
gohgwaja 0:7cff999a7f5c 3
gohgwaja 0:7cff999a7f5c 4 #include "spi_setup.h"
gohgwaja 0:7cff999a7f5c 5
gohgwaja 0:7cff999a7f5c 6 #define MCP_GPPUA 0x0C // PULL_UP Port A
gohgwaja 0:7cff999a7f5c 7 #define MCP_GPPUB 0x0D // PULL_UP Port B
gohgwaja 0:7cff999a7f5c 8
gohgwaja 0:7cff999a7f5c 9 #define MCP_IODIRA 0x00 // DIR Port A
gohgwaja 0:7cff999a7f5c 10 #define MCP_IODIRB 0x01 // DIR Port B
gohgwaja 0:7cff999a7f5c 11 #define MCP_GPPUA 0x0C // Pull up Port A
gohgwaja 0:7cff999a7f5c 12 #define MCP_GPPUB 0x0D // Pull up Port B
gohgwaja 0:7cff999a7f5c 13 #define MCP_GPIOA 0x12 // Address Port A
gohgwaja 0:7cff999a7f5c 14 #define MCP_GPIOB 0x13 // Address Port B
gohgwaja 0:7cff999a7f5c 15 #define MCP_WRITE_BYTE 0x40
gohgwaja 0:7cff999a7f5c 16 #define MCP_READ_BYTE 0x41
gohgwaja 0:7cff999a7f5c 17
gohgwaja 0:7cff999a7f5c 18 #define MCP_SEG_CS (uint8_t)0
gohgwaja 0:7cff999a7f5c 19 #define MCP_BTN1_CS (uint8_t)1
gohgwaja 0:7cff999a7f5c 20 #define MCP_BTN2_CS (uint8_t)2
gohgwaja 0:7cff999a7f5c 21
gohgwaja 0:7cff999a7f5c 22 void MCP23S17_Init();
gohgwaja 0:7cff999a7f5c 23 void MCP_Write(uint8_t cs_select, uint8_t reg_addr, uint8_t data);
gohgwaja 0:7cff999a7f5c 24 uint8_t MCP_Read(uint8_t cs_select, uint8_t reg_addr);
gohgwaja 0:7cff999a7f5c 25
gohgwaja 0:7cff999a7f5c 26 void MCP23S17_Init()
gohgwaja 0:7cff999a7f5c 27 {
gohgwaja 0:7cff999a7f5c 28 MCP_Write(MCP_SEG_CS, MCP_IODIRA, 0x00); //GPIOA as output A
gohgwaja 0:7cff999a7f5c 29 MCP_Write(MCP_SEG_CS, MCP_IODIRB, 0x00); //GPIOB as output B
gohgwaja 0:7cff999a7f5c 30
gohgwaja 0:7cff999a7f5c 31 MCP_Write(MCP_BTN1_CS, MCP_IODIRA, 0xff); //GPIOA as input A
gohgwaja 0:7cff999a7f5c 32 MCP_Write(MCP_BTN1_CS, MCP_IODIRB, 0xff); //GPIOB as input B
gohgwaja 0:7cff999a7f5c 33
gohgwaja 0:7cff999a7f5c 34 MCP_Write(MCP_BTN2_CS, MCP_IODIRA, 0xff); //GPIOA as input A
gohgwaja 0:7cff999a7f5c 35 MCP_Write(MCP_BTN2_CS, MCP_IODIRB, 0xff); //GPIOB as input B
gohgwaja 0:7cff999a7f5c 36
gohgwaja 0:7cff999a7f5c 37 MCP_Write(MCP_BTN2_CS, MCP_GPPUB, 0xff); //GPIOB as input B
gohgwaja 0:7cff999a7f5c 38 }
gohgwaja 0:7cff999a7f5c 39
gohgwaja 0:7cff999a7f5c 40 void MCP_Write(uint8_t cs_select, uint8_t reg_addr, uint8_t data)
gohgwaja 0:7cff999a7f5c 41 {
gohgwaja 0:7cff999a7f5c 42 if(cs_select == MCP_SEG_CS) seg_cs = 1;
gohgwaja 0:7cff999a7f5c 43 else if(cs_select == MCP_BTN1_CS) btn1_cs = 1;
gohgwaja 0:7cff999a7f5c 44 else if(cs_select == MCP_BTN2_CS) btn2_cs = 1;
gohgwaja 0:7cff999a7f5c 45
gohgwaja 0:7cff999a7f5c 46 spi1.write(MCP_WRITE_BYTE);
gohgwaja 0:7cff999a7f5c 47 spi1.write(reg_addr);
gohgwaja 0:7cff999a7f5c 48 spi1.write(data);
gohgwaja 0:7cff999a7f5c 49
gohgwaja 0:7cff999a7f5c 50 if(cs_select == MCP_SEG_CS) seg_cs = 0;
gohgwaja 0:7cff999a7f5c 51 else if(cs_select == MCP_BTN1_CS) btn1_cs = 0;
gohgwaja 0:7cff999a7f5c 52 else if(cs_select == MCP_BTN2_CS) btn2_cs = 0;
gohgwaja 0:7cff999a7f5c 53 }
gohgwaja 0:7cff999a7f5c 54
gohgwaja 0:7cff999a7f5c 55 uint8_t MCP_Read(uint8_t cs_select, uint8_t reg_addr)
gohgwaja 0:7cff999a7f5c 56 {
gohgwaja 0:7cff999a7f5c 57 uint8_t receive_data = 0;
gohgwaja 0:7cff999a7f5c 58
gohgwaja 0:7cff999a7f5c 59 if(cs_select == MCP_SEG_CS) seg_cs = 1;
gohgwaja 0:7cff999a7f5c 60 else if(cs_select == MCP_BTN1_CS) btn1_cs = 1;
gohgwaja 0:7cff999a7f5c 61 else if(cs_select == MCP_BTN2_CS) btn2_cs = 1;
gohgwaja 0:7cff999a7f5c 62
gohgwaja 0:7cff999a7f5c 63 spi1.write(MCP_READ_BYTE);
gohgwaja 0:7cff999a7f5c 64 spi1.write(reg_addr);
gohgwaja 0:7cff999a7f5c 65 receive_data = spi1.write(0x00);
gohgwaja 0:7cff999a7f5c 66
gohgwaja 0:7cff999a7f5c 67 if(cs_select == MCP_SEG_CS) seg_cs = 0;
gohgwaja 0:7cff999a7f5c 68 else if(cs_select == MCP_BTN1_CS) btn1_cs = 0;
gohgwaja 0:7cff999a7f5c 69 else if(cs_select == MCP_BTN2_CS) btn2_cs = 0;
gohgwaja 0:7cff999a7f5c 70
gohgwaja 0:7cff999a7f5c 71 return receive_data;
gohgwaja 0:7cff999a7f5c 72 }
gohgwaja 0:7cff999a7f5c 73
gohgwaja 0:7cff999a7f5c 74
gohgwaja 0:7cff999a7f5c 75 #endif