|
楼主 |
发表于 2013-4-9 10:07:17
|
显示全部楼层
spydf 发表于 2012-8-10 16:57
找到原因了,fac_ms是16位的.而延时1ms的值是l100000,溢出了. 利用位带实现io口位操作你实现没有?我打算接下 ...
/* The processor memory map includes two bit-band regions. These occupy the lowest
* 1MB of the SRAM and peripheral memory regions respectively.
* + SRAM: Bit-band region: 0x20000000 - 0x20100000
* Bit-band alias: 0x22000000 - 0x23FFFFFF
* + PERI: Bit-band region: 0x40000000 - 0x40100000
* Bit-band alias: 0x42000000 - 0x43FFFFFF
* The mapping formula:
* bit_word_offset = (byte_offset * 32) + (bit_number * 4)
* bit_word_address = bit_band_base + bit_word_offset
* where:
* + bit_word_offset: the position of the target bit in the bit-band memory region
* + bit_word_addr: the address of the word in the alias memory region that maps to the target bit
* + bit_band_base: the starting address of the alias region
* + byte_offset: the number of byte in the bit-band region that contains the targeted bit
* + bit_number: is the bit position (0-7) of the targeted bit
*
* Note: The fact, the SRAM on LPC1768 just available in two ranges:
* + 0x2007C000 - 0x2007FFFF: for SRAM - bank 0
* + 0x20080000 - 0x20083FFF: for SRAM - bank 1
* (And one range for GPIO peripheral but assigned in SRAM ranges: 0x2009C000 - 0x2009FFFF)
*/
/* Bit band SRAM definitions */
#define BITBAND_SRAM_REF 0x20000000
#define BITBAND_SRAM_BASE 0x22000000
#define BITBAND_SRAM(a,b) ((BITBAND_SRAM_BASE + ((a-BITBAND_SRAM_REF)<<5) + (b<<2))) // Convert SRAM address
/* Bit band PERIPHERAL definitions */
#define BITBAND_PERI_REF 0x40000000
#define BITBAND_PERI_BASE 0x42000000
#define BITBAND_PERI(a,b) ((BITBAND_PERI_BASE + ((a-BITBAND_PERI_REF)<<5) + (b<<2))) // Convert PERI address
/* Basic bit band function definitions */
#define BITBAND_SRAM_ClearBit(a,b) (*(volatile uint32_t *) (BITBAND_SRAM(a,b)) = 0)
#define BITBAND_SRAM_SetBit(a,b) (*(volatile uint32_t *) (BITBAND_SRAM(a,b)) = 1)
#define BITBAND_SRAM_GetBit(a,b) (*(volatile uint32_t *) (BITBAND_SRAM(a,b)))
#define BITBAND_PERI_ClearBit(a,b) (*(volatile uint32_t *) (BITBAND_PERI(a,b)) = 0)
#define BITBAND_PERI_SetBit(a,b) (*(volatile uint32_t *) (BITBAND_PERI(a,b)) = 1)
#define BITBAND_PERI_GetBit(a,b) (*(volatile uint32_t *) (BITBAND_PERI(a,b)))
//volatile unsigned long bbVarAry[7] __attribute__(( at(0x20003014) ));
//volatile unsigned long* const pbbaVar= (void*)(0x22000000+0x3014*8*4);
//位带操作,实现51类似的GPIO控制功能
//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
//IO口操作宏定义
//0x2000_0000-0x200F_FFFF(SRAM区中的最低1MB)
//对于SRAM位带区的某个比特,记它所在字节地址为A,位序号为n(0<=n<=7),则该比特在别名区的地址为:
//AliasAddr= 0x22000000+((A-0x20000000)*8+n)*4 =0x22000000+ (A-0x20000000)*32 + n*4
//0x4000_0000-0x400F_FFFF(片上外设区中的最低1MB)
//对于片上外设位带区的某个比特,记它所在字节的地址为A,位序号为n(0<=n<=7),则该比特在别名区的地址为:
//AliasAddr= 0x42000000+((A-0x40000000)*8+n)*4 =0x42000000+ (A-0x40000000)*32 + n*4
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
//FIODIR
#define LPC_GPIO0_DIR_Addr (0x2009c000) //
#define LPC_GPIO1_DIR_Addr (0x2009c020) //
#define LPC_GPIO2_DIR_Addr (0x2009c040) //
#define LPC_GPIO3_DIR_Addr (0x2009c060) //
#define LPC_GPIO4_DIR_Addr (0x2009c080) //
//FIOMASK
#define LPC_GPIO0_MASK_Addr (0x2009c010) //
#define LPC_GPIO1_MASK_Addr (0x2009c030) //
#define LPC_GPIO2_MASK_Addr (0x2009c050) //
#define LPC_GPIO3_MASK_Addr (0x2009c070) //
#define LPC_GPIO4_MASK_Addr (0x2009c090) //
//FIOPIN
#define LPC_GPIO0_PIN_Addr (0x2009c014) //
#define LPC_GPIO1_PIN_Addr (0x2009c034) //
#define LPC_GPIO2_PIN_Addr (0x2009c054) //
#define LPC_GPIO3_PIN_Addr (0x2009c074) //
#define LPC_GPIO4_PIN_Addr (0x2009c094) //
//FIOSET
#define LPC_GPIO0_SET_Addr (0x2009c018) //
#define LPC_GPIO1_SET_Addr (0x2009c038) //
#define LPC_GPIO2_SET_Addr (0x2009c058) //
#define LPC_GPIO3_SET_Addr (0x2009c078) //
#define LPC_GPIO4_SET_Addr (0x2009c098) //
//FIOCLR
#define LPC_GPIO0_CLR_Addr (0x2009c01C) //
#define LPC_GPIO1_CLR_Addr (0x2009c03C) //
#define LPC_GPIO2_CLR_Addr (0x2009c05C) //
#define LPC_GPIO3_CLR_Addr (0x2009c07C) //
#define LPC_GPIO4_CLR_Addr (0x2009c09C) //
///================================================
//FIODIR
#define P0DIR(n) BIT_ADDR(LPC_GPIO0_DIR_Addr,n)
#define P1DIR(n) BIT_ADDR(LPC_GPIO1_DIR_Addr,n)
#define P2DIR(n) BIT_ADDR(LPC_GPIO2_DIR_Addr,n)
#define P3DIR(n) BIT_ADDR(LPC_GPIO3_DIR_Addr,n)
#define P4DIR(n) BIT_ADDR(LPC_GPIO4_DIR_Addr,n)
//FIOMASK
#define P0MASK(n) BIT_ADDR(LPC_GPIO0_MASK_Addr,n)
#define P1MASK(n) BIT_ADDR(LPC_GPIO1_MASK_Addr,n)
#define P2MASK(n) BIT_ADDR(LPC_GPIO2_MASK_Addr,n)
#define P3MASK(n) BIT_ADDR(LPC_GPIO3_MASK_Addr,n)
#define P4MASK(n) BIT_ADDR(LPC_GPIO4_MASK_Addr,n)
//FIOPIN
#define P0PIN(n) BIT_ADDR(LPC_GPIO0_PIN_Addr,n)
#define P1PIN(n) BIT_ADDR(LPC_GPIO1_PIN_Addr,n)
#define P2PIN(n) BIT_ADDR(LPC_GPIO2_PIN_Addr,n)
#define P3PIN(n) BIT_ADDR(LPC_GPIO3_PIN_Addr,n)
#define P4PIN(n) BIT_ADDR(LPC_GPIO4_PIN_Addr,n)
//FIOSET
#define P0SET(n) BIT_ADDR(LPC_GPIO0_SET_Addr,n)
#define P1SET(n) BIT_ADDR(LPC_GPIO1_SET_Addr,n)
#define P2SET(n) BIT_ADDR(LPC_GPIO2_SET_Addr,n)
#define P3SET(n) BIT_ADDR(LPC_GPIO3_SET_Addr,n)
#define P4SET(n) BIT_ADDR(LPC_GPIO4_SET_Addr,n)
//FIOCLR
#define P0CLR(n) BIT_ADDR(LPC_GPIO0_CLR_Addr,n)
#define P1CLR(n) BIT_ADDR(LPC_GPIO1_CLR_Addr,n)
#define P2CLR(n) BIT_ADDR(LPC_GPIO2_CLR_Addr,n)
#define P3CLR(n) BIT_ADDR(LPC_GPIO3_CLR_Addr,n)
#define P4CLR(n) BIT_ADDR(LPC_GPIO4_CLR_Addr,n)
|
|