pcf8574-blinky v2
This commit is contained in:
		
							parent
							
								
									4baa62ef9a
								
							
						
					
					
						commit
						5db0144d4c
					
				
					 4 changed files with 270 additions and 0 deletions
				
			
		
							
								
								
									
										17
									
								
								myapps/misc/pcf8574-blinky-refac/src/inc/pcf8574.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								myapps/misc/pcf8574-blinky-refac/src/inc/pcf8574.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| // PCF8574 driver header
 | ||||
| 
 | ||||
| 
 | ||||
| #define PCFP0 0x01 | ||||
| #define PCFP1 0x02 | ||||
| #define PCFP2 0x04 | ||||
| #define PCFP3 0x08 | ||||
| #define PCFP4 0x10 | ||||
| #define PCFP5 0x20 | ||||
| #define PCFP6 0x40 | ||||
| #define PCFP7 0x80   //128
 | ||||
| 
 | ||||
| void pcf_init(); | ||||
| 
 | ||||
| void pcf_setbits(uint8_t ports); | ||||
| void pcf_resetbits(uint8_t ports); | ||||
| 
 | ||||
							
								
								
									
										24
									
								
								myapps/misc/pcf8574-blinky-refac/src/main.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								myapps/misc/pcf8574-blinky-refac/src/main.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,24 @@ | |||
| // LED on PORT0
 | ||||
| 
 | ||||
| 
 | ||||
| #include "CH59x_common.h" | ||||
| #include "pcf8574.h" | ||||
| 
 | ||||
| 
 | ||||
| int main() | ||||
| { | ||||
|   SetSysClock(CLK_SOURCE_PLL_60MHz); | ||||
|    | ||||
|   pcf_init(); | ||||
|    | ||||
|   pcf_setbits(PCFP0); | ||||
|    | ||||
|   while (1) { | ||||
|     mDelaymS(400); | ||||
|     pcf_resetbits(PCFP0); | ||||
|      | ||||
|     mDelaymS(100); | ||||
|     pcf_setbits(PCFP0);     | ||||
|   } | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										62
									
								
								myapps/misc/pcf8574-blinky-refac/src/pcf8574.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								myapps/misc/pcf8574-blinky-refac/src/pcf8574.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,62 @@ | |||
| // PCF8574 driver
 | ||||
| 
 | ||||
| #include "CH59x_common.h" | ||||
| #include "pcf8574.h" | ||||
| 
 | ||||
| #define I2C_OWNADD  0x0001 | ||||
| 
 | ||||
| 
 | ||||
| void pcf_init() | ||||
| { | ||||
|   I2C_Init(I2C_Mode_I2C, 100000, I2C_DutyCycle_16_9, | ||||
|               I2C_Ack_Enable, I2C_AckAddr_7bit, I2C_OWNADD); | ||||
|   I2C_GenerateSTART(ENABLE); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); | ||||
|   I2C_Send7bitAddress(0x40, I2C_Direction_Transmitter); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); | ||||
|   I2C_SendData(0xFF); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)); | ||||
|   I2C_GenerateSTOP(ENABLE); | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  * set the ports OR'd together like: PCFP0 | PCFP1 | ||||
|  */ | ||||
| void pcf_setbits(uint8_t ports) | ||||
| { | ||||
|   I2C_GenerateSTART(ENABLE); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); | ||||
|   I2C_Send7bitAddress(0x41, I2C_Direction_Receiver); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); | ||||
|   uint8_t curr = I2C_ReceiveData(); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)); | ||||
|   I2C_GenerateSTOP(ENABLE); | ||||
|    | ||||
|   I2C_GenerateSTART(ENABLE); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); | ||||
|   I2C_Send7bitAddress(0x40, I2C_Direction_Transmitter); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); | ||||
|   I2C_SendData(curr | ports); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)); | ||||
|   I2C_GenerateSTOP(ENABLE); | ||||
| } | ||||
| 
 | ||||
| void pcf_resetbits(uint8_t ports) | ||||
| { | ||||
|   I2C_GenerateSTART(ENABLE); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); | ||||
|   I2C_Send7bitAddress(0x41, I2C_Direction_Receiver); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); | ||||
|   uint8_t curr = I2C_ReceiveData(); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)); | ||||
|   I2C_GenerateSTOP(ENABLE); | ||||
|    | ||||
|   I2C_GenerateSTART(ENABLE); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); | ||||
|   I2C_Send7bitAddress(0x40, I2C_Direction_Transmitter); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); | ||||
|   I2C_SendData(curr & ~(ports)); | ||||
|   while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)); | ||||
|   I2C_GenerateSTOP(ENABLE); | ||||
| } | ||||
| 
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue