2013年9月6日 星期五

NETMF Character LCD (C# Code)

I found a usable C# sorec code  for HD44780-compatible Character LCD.
The  BitBucket open source netmf LCD page . has NETMF source code for your projcet.
The online LCD simulator is very useful for you to verify code that you send to LCD.


2013年9月4日 星期三

NETMF C# hardware Watchdog for FEZ Cerb40

Hardware watchdog is very important in industrial control. The FEZ Cerberus main board   /FEZ Cerb40 module are good choose for low cost design. Unfortunately, it is not support watchdog in firmware! (also, The GHI guy said :  Open Source "OSHW" is not support!)

I read the STM32F405 reference manual to find out the solution ... And I try to write the low leve CPU register setting to enable watchdog ...  It is working ! 

The C# source code as follows :
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware.LowLevel;

 class iWatchDog
    {
        public uint  TimeOut = 4000; // default 4000 units    
        private uint AccKey = 0x5555;
        private uint RstKey = 0xAAAA;
        private uint EnableKey = 0xCCCC;
        //----------------------------------------------------------------
        Register IWDG_KR  = new Register(0x40003000);
        Register IWDG_PR  = new Register(0x40003004);
        Register IWDG_RLR = new Register(0x40003008);
        Register IWDG_SR  = new Register(0x4000300C);

        //-----------------------------------------------------------------
        public void Init()
        {
            IWDG_KR.Write(AccKey);   // open the access key
            IWDG_PR.Write(4);        // write the Prescaler for 2ms (One unit)
            IWDG_KR.Write(AccKey);   // open again
            IWDG_RLR.Write(TimeOut); // write the down counter  

        }
        //--------------------------------------------------------------
        public void Enable()
        {
            IWDG_KR.Write(AccKey);    // open access key
            IWDG_KR.Write(EnableKey); // enable counter
        }
        //---------------------------------------------------------------
        public void Reset()
        {
            IWDG_KR.Write(AccKey);    // open access key
            IWDG_KR.Write(RstKey);    // reload counter
        }

    }


//--------------------------------------------
// your main program as follows:
//--------------------------------------------.
.
 iWatchDog Wdog = new iWatchDog();
 Wdog.TimeOut = 2500; // for 5 second time out
 Wdog.Init();
 Wdog.Enable();  // enable watch dog

.
.
//-------- the easy way to reset the watchdos counter -----------------
  void timer500_Tick(GT.Timer timer)// 500mS timer tick
  {


             Wdog.Reset(); // reload the watchdog timeout counter
  }

2013年9月3日 星期二

I2C for EEPROM

The  Cerb Family has one I2C interface that connected to socket1 and socket2. I found a very usable code from GHI Electronics . I test the sorce code for 24LC16 eeprom and it is working.
The source code as follows: 
 
//-------------- setup the I2C device ----------------------
I2CDevice EepI2C = new I2CDevice(new I2CDevice.Configuration(0x50, 400)); // 24LC16 Chip @400KHZ clock

//--------------- Write to EEPROM -----------------------
public void EepWrite(int Address, byte data)
        {
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address &                 0xFF),data });
            EepI2C.Execute(xActions, 1000);
            Thread.Sleep(5); // Mandatory after each Write transaction !!!
        }

//----------- Read from EEPROM ----------------------
public byte EepRead(int Address)
        {
            var Data = new byte[1];
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { (byte)(Address >> 8), (byte)(Address & 0xFF) });
            EepI2C.Execute(xActions, 1000);
            Thread.Sleep(5);   // Mandatory after each Write transaction !!!
            xActions[0] = I2CDevice.CreateReadTransaction(Data);
            EepI2C.Execute(xActions, 1000);
            return Data[0];
        }

2013年9月2日 星期一

NETMF Cpu.Pin / PWM Channel /Analog Channel

A usable reference :
NETMF pin map
Cpu.Pin
Port
Cpu.Pin
Port
Cpu.Pin
Port
0
PA0
32
PC0
64
PE0
1
PA1
33
PC1
65
PE1
2
PA2
34
PC2
66
PE2
3
PA3
35
PC3
67
PE3
4
PA4
36
PC4
68
PE4
5
PA5
37
PC5
69
PE5
6
PA6
38
PC6
70
PE6
7
PA7
39
PC7
71
PE7
8
PA8
40
PC8
72
PE8
9
PA9
41
PC9
 
 
10
PA10
42
PC10
 
 
11
PA11
43
PC11
 
 
12
PA12
44
PC12
 
 
13
PA13
45
PC13
 
 
14
PA14
46
PC14
 
 
15
PA15
47
PC15
 
 
16
PB0
48
PD0
 
 
17
PB1
49
PD1
 
 
18
PB2
50
PD2
 
 
19
PB3
51
PD3
 
 
20
PB4
52
PD4
 
 
21
PB5
53
PD5
 
 
22
PB6
54
PD6
 
 
23
PB7
55
PD7
 
 
24
PB8
56
PD8
 
 
25
PB9
57
PD9
 
 
26
PB10
58
PD10
 
 
27
PB11
59
PD11
 
 
28
PB12
60
PD12
 
 
29
PB13
61
PD13
 
 
30
PB14
62
PD14
 
 
31
PB15
63
PD15
 
 

C# Example:   
LED   = new OutputPort((Cpu.Pin)5, true);  //PA5
// Assign the I/O port PA5 as a output port for LED display.

PWM CHANNEL
Physical Pin
 
ANALOG  CHANNEL
Physical Pin
0
PC6
 
0
PA6
1
PA7
 
1
PA2
2
PC7
 
2
PA3
3
PA8
 
3
PC0
4
PB0
 
4
PC1
5
PB1
 
5
PA4
6
PB5
 
6
PC2
7
PB4
 
7
PC3
8
PB3
 
8
PA5
9
PB11
 
9
PB0
10
PB10
 
10
PB1
11
PA10
 
 
 
12
PA9
 
 
 
13
PA15
 
 
 
14
PB8
 
 
 
15
PB9
 
 
 

C# Example :
 AnalogInput Sensor1 = new AnalogInput(Cpu.AnalogChannel.ANALOG_3);
// Assign the Analog input channel 3 as a Sensor1
  PWM Moto1Pwm = new PWM(Cpu.PWMChannel.PWM_0, 10000, 0.5, false);
// Assign PWM channel 0 as Moto1Pwm output  and working at 10KHz, 50% duty cycle