Предлагаю свой вариант работы с портами. В основном писалось ради удобства конфигурирования, управление использую только если нужно динамически менять пин. Для хранения информации о пине хватило 1 байта: младшие 4 бита - номер пина, старшие 4 - номер порта, 0 - пин не присвоен. Для конфигурации создал энумератор и там перечислены все настройки.
typedef enum { gpInputMode = 0x0000, gpGeneralMode = 0x0001, gpAlternateMode = 0x0002, gpAnalogMode = 0x0003, gpPushPull = 0x0000, gpOpenDrain = 0x0004, gpSpeedLow = 0x0000, gpSpeedMedium = 0x0008, gpSpeedHigh = 0x10, gpSpeedVeryHigh = 0x18, gpNoPull = 0x0000, gpPullUp = 0x0020, gpPullDown = 0x0040, gpODR_Low = 0x0000, gpODR_High = 0x0080, gpAF0 = 0x0000, gpAF1 = 0x0100, gpAF2 = 0x0200, gpAF3 = 0x0300, gpAF4 = 0x0400, gpAF5 = 0x0500, gpAF6 = 0x0600, gpAF7 = 0x0700, gpAF8 = 0x0800, gpAF9 = 0x0900, gpAF10 = 0x0a00, gpAF11 = 0x0b00, gpAF12 = 0x0c00, gpAF13 = 0x0d00, gpAF14 = 0x0e00, gpAF15 = 0x0f00 /* 0:1 MODER 2 OTYPER 3:4 OSPEEDR 5:6 PUPDR 7: ODR 8:15 AFx */ } Typ_CtrlPort;
перечисление пинов:
typedef enum { P_NC = 0, PA0 = 0x10, PA1 = 0x11, PA2 = 0x12, PA3 = 0x13, PA4 = 0x14, PA5 = 0x15, PA6 = 0x16, PA7 = 0x17, PA8 = 0x18, PA9 = 0x19, PA10 = 0x1a, PA11 = 0x1b, PA12 = 0x1c, PA13 = 0x1d, PA14 = 0x1e, PA15 = 0x1f, PB0 = 0x20, PB1 = 0x21, PB2 = 0x22, PB3 = 0x23, PB4 = 0x24, PB5 = 0x25, PB6 = 0x26, PB7 = 0x27, PB8 = 0x28, PB9 = 0x29, PB10 = 0x2a, PB11 = 0x2b, PB12 = 0x2c, PB13 = 0x2d, PB14 = 0x2e, PB15 = 0x2f, PC0 = 0x30, PC1 = 0x31, PC2 = 0x32, PC3 = 0x33, PC4 = 0x34, PC5 = 0x35, PC6 = 0x36, PC7 = 0x37, PC8 = 0x38, PC9 = 0x39, PC10 = 0x3a, PC11 = 0x3b, PC12 = 0x3c, PC13 = 0x3d, PC14 = 0x3e, PC15 = 0x3f, PD0 = 0x40, PD1 = 0x41, PD2 = 0x42, PD3 = 0x43, PD4 = 0x44, PD5 = 0x45, PD6 = 0x46, PD7 = 0x47, PD8 = 0x48, PD9 = 0x49, PD10 = 0x4a, PD11 = 0x4b, PD12 = 0x4c, PD13 = 0x4d, PD14 = 0x4e, PD15 = 0x4f, PE0 = 0x50, PE1 = 0x51, PE2 = 0x52, PE3 = 0x53, PE4 = 0x54, PE5 = 0x55, PE6 = 0x56, PE7 = 0x57, PE8 = 0x58, PE9 = 0x59, PE10 = 0x5a, PE11 = 0x5b, PE12 = 0x5c, PE13 = 0x5d, PE14 = 0x5e, PE15 = 0x5f, PF0 = 0x60, PF1 = 0x61, PF2 = 0x62, PF3 = 0x63, PF4 = 0x64, PF5 = 0x65, PF6 = 0x66, PF7 = 0x67, PF8 = 0x68, PF9 = 0x69, PF10 = 0x6a, PF11 = 0x6b, PF12 = 0x6c, PF13 = 0x6d, PF14 = 0x6e, PF15 = 0x6f, PG0 = 0x70, PG1 = 0x71, PG2 = 0x72, PG3 = 0x73, PG4 = 0x74, PG5 = 0x75, PG6 = 0x76, PG7 = 0x77, PG8 = 0x78, PG9 = 0x79, PG10 = 0x7a, PG11 = 0x7b, PG12 = 0x7c, PG13 = 0x7d, PG14 = 0x7e, PG15 = 0x7f, PH0 = 0x80, PH1 = 0x81, PH2 = 0x82, PH3 = 0x83, PH4 = 0x84, PH5 = 0x85, PH6 = 0x86, PH7 = 0x87, PH8 = 0x88, PH9 = 0x89, PH10 = 0x8a, PH11 = 0x8b, PH12 = 0x8c, PH13 = 0x8d, PH14 = 0x8e, PH15 = 0x8f, PI0 = 0x90, PI1 = 0x91, PI2 = 0x92, PI3 = 0x93, PI4 = 0x94, PI5 = 0x95, PI6 = 0x96, PI7 = 0x97, PI8 = 0x98, PI9 = 0x99, PI10 = 0x9a, PI11 = 0x9b, PI12 = 0x9c, PI13 = 0x9d, PI14 = 0x9e, PI15 = 0x9f // младшие 4 бита - номер пина 0 - 15 // старшие 4 бита - номер порта 1 - 4, 0 - не назначен } Typ_Port;
Отредактировано MasterElectric (2018-03-31 18:59:29)