Pages

2011-04-27

Microchip PIC: Using TIMER0

In this memo Timer 0 is operated in 8-bit mode. The time for an interrupt is given by:


where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period.

In our application the clock frequency is 4MHz, that is, clock period = 0.25µs, and Time = 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0Lcan be calculated as follows:
Thus, TMR0L should be loaded with 100. The value to be loaded into TMR0 control register T0CON can then be found as:
Thus, T0CON register should be loaded with hexadecimal 0xC4. The next register to be configured is the interrupt control register INTCON, where we will disable priority based interrupts and enable the global interrupts and TMR0 interrupts:
Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded intoregister INTCON is 0xA0.

When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 0x20 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag.

The operations to be performed can thus be summarized as follows:
In the main program:
  • Load TMR0L with 100
  • Set T0CON to 0xC4
  • Set INTCON to 0xA0
  • Increment the counter with 1-second delays
In the interrupt service routine:
  • Re-load TMR0L to 100
  • Refresh displays
  • Set INTCON to 0x20 (reenable TMR0 interrupts and clear timer interrupt flag)
Source code example:

      2011-04-08

      DotNet: Using Sockets in C#

      • Using TcpListener for creating a TCP server:
      using System;
      using System.Threading;
      using System.Net;
      using System.Net.Sockets;
      using System.IO;
      private Socket connection; // Socket for accepting a connection
      private Thread readThread; // Thread for processing incoming messages
      private NetworkStream socketStream; // network data stream
      private BinaryWriter writer; // facilitates writing to the stream
      private BinaryReader reader; // facilitates reading from the stream
      readThread = new Thread( new ThreadStart( RunServer ) );
      readThread.Start();
      public void RunServer()
      {

      TcpListener listener; 
      // Step 1: create TcpListenerIPAddress local = IPAddress.Parse( "127.0.0.1" );listener = new TcpListener( local, 50000 ); 
      // Step 2: TcpListener waits for connection requestlistener.Start(); 
      // Step 3: establish connection upon client requestwhile ( true ){
      // accept an incoming connectionconnection = listener.AcceptSocket(); 
      // create NetworkStream object associated with socketsocketStream = new NetworkStream( connection ); 
      // create objects for transferring data across streamwriter = new BinaryWriter( socketStream );reader = new BinaryReader( socketStream ); 
      // Step 4: read string data sent from client 

      // Step 5: close connectionwriter.Close();reader.Close();socketStream.Close();connection.Close();
      }
      }






      2011-03-31

      SQL Server: DB Restore

      • Restoring a DB:
      RESTORE DATABASE AdventureWorks
      FROM AdWorksDevice
      WITH RECOVERY
      • Restoring Differential Backups in Simple Recovery Mode
      RESTORE DATABASE AdventureWorks
      FROM DISK='D:\SQLBackups\AWFull.bak'
      WITH NORECOVERY -- We aren't done with our Restore process
       Then
      RESTORE DATABASE AdventureWorks
      FROM DISK='D:\SQLBackups\AWDiff.bak'
      WITH NORECOVERY
      Finally
      RESTORE DATABASE AdventureWorks
      WITH RECOVERY

      SQL Server: DB Backup - Part 2

      • Creating a Backup Device
      EXEC sp_adddumpdevice @devtype='disk',
      @logicalname='AWBackup',
      @physicalname='D:\backup\AWBackup1.bak'

      EXEC sp_adddumpdevice @devtype='tape',
      @logicalname='AWTapeBackup',
      @physicalname='\\.\tape0'
      • Removing a Backup Device
      Sp_dropdevice @logicalname='AWBackup'  
      This command only removes the backup device definition—it doesn’t automatically delete the files contained therein. Use this command to remove all the files in addition tothe device definition: 
      Sp_dropdevice @logicalname='AWBackup', @devfile='DELFILE'
      • Managing Backup Devices
      BACKUP DATABASE AdventureWorks
      TO AWBackup
      WITH EXPIREDATE = '12/05/2007'
      or
      BACKUP DATABASE AdventureWorks
      TO AWBackup
      WITH RETAINDAYS=7
      The first option causes that specific backup to be removed after December 5, 2007at midnight. The second option forces that particular backup to be
      removed after seven days have passed.

      2011-03-10

      SQL Server: DB Backup

      • Transaction log backup:
      USE [master]
      BACKUP LOG [AdventureWorks]
      TO DISK = N'C:\AdventureWorks_LOG.TRN' WITH NOFORMAT,
      NOINIT,
      NAME = N'AdventureWorks-
      Transaction log backup.BAK'
      GO
      • Backup data file:
      USE [master]
      BACKUP DATABASE [AdventureWorks]
      TO DISK = N'C:\AdventureWorks_DATAS.BAK' WITH NOFORMAT,
      NOINIT,
      NAME = N'AdventureWorks-Backup data file.BAK'
      GO
      • Differential Backup
       In this example, AW.dif contains all the changes made since the last full backup. You can use it during the restore process in addition to transaction log backups. First, restore the full backup, then restore the latest differential, then restore any transaction logs that
      follow it.
      BACKUP DATABASE Adventureworks TO DISK = 'D:\data\AW.dif' WITH DIFFERENTIAL,INIT
      • Mirrored Backup (SQL Server 2005)
      BACKUP DATABASE AdventureWorks TO DISK='D:\data\AW.bak
      MIRROR TO DISK = 'E:\data\AW.bak'
      MIRROR TO DISK = 'F:\data\AW.bak'
      WITH INIT,CHECKSUM,CONTINUE_ON_ERROR
      •  BATCH Script
      sqlcmd
      -S MYSERVER\SQLEXPRESSINSTANCENAME -U sa -P MYPASSWORD
      -Q "BACKUP DATABASE [AdventureWorks] TO DISK = N'C:\AdventureWorks_DATAS.BAK'
      WITH NOFORMAT, NOINIT,
      NAME = N'AdventureWorks-Backup.BAK'"
      Or
      SET J=%date:~-10,2%
      SET A=%date:~-4%
      SET M=%date:~-7,2%
      SET H=%time:~0,2%
      SET MN=%time:~3,2%
      SET S=%time:~-5,2%

      IF "%time:~0,1%"==" " SET H=0%HEURE:~1,1%

      SET REPERTOIRE=C:\Repertoire_Sauvegarde\

      SET FICHIER=%REPERTOIRE%\Nom_de_mon_fichier_%J%_%M%_%A%_A_%H%_%MN%_%S%.bak

      IF NOT exist "%REPERTOIRE%" md "%REPERTOIRE%"

      cd C:\Program Files\Microsoft SQL Server\90\Tools\Binn

      sqlcmd -S NOM_DU_SERVEUR\SQLEXPRESS -Q "BACKUP DATABASE NOM_DE_LA BASE TO DISK = N'%FICHIER%' WITH INIT, NAME = N'Sauvegarde automatique de la base de données', STATS = 1" 

      2011-03-08

      C++: Using Inserter Iterators

      An inserter iterator is an iterator that can add new elements to the sequence containers vector < T > ,
      deque < T > , and list < T > . There are three templates that create inserter iterators:
      •  back_insert_iterator < T > — inserts elements at the end of a container of type T . The container
        must provide the push_back() function for this to work.
      • front_insert_iterator < T > — inserts elements at the beginning of a container of type T .
        This depends on push_front() being available for the container.
      • insert_iterator < T > — inserts elements starting at a specifi ed position within a container
        of type T . This requires that the container has an insert() function that accepts an iterator
        as the fi rst argument, and an item to be inserted as the second argument.
      The constructors for the fi rst two types of inserter iterators expect a single argument specifying the
      container in which elements are to be inserted. For example:
      list < int > numbers;
      front_insert_iterator < list < int > > iter(numbers);
      *iter = 99;  // front_inserter(numbers) = 99;
      The constructor for an insert_iterator < T > iterator requires two arguments:
      insert_iterator < vector < int > > iter_anywhere(numbers, numbers.begin());
      The second argument to the constructor is an iterator specifying where data is to be inserted — the
      start of the sequence, in this instance. You can use this iterator in exactly the same way as
      the previous one. Here ’ s how you could insert a series of values into a vector container using this iterator:
      for(int i = 0 ; i < 100 ; i++)
      *iter_anywhere = i + 1;
      This loop inserts the values from 1 to 100 in the numbers container at the beginning. After
      executing this, the fi rst 100 elements will be 100 , 99 , and so on, through to 1 .
      You could also use the inserter() function to achieve the same result:
      for(int i = 0 ; i < 100 ; i++)
      inserter(numbers, numbers.begin()) = i + 1;
      The first argument is the container, and the second is an iterator identifying the
      position where data is to be inserted. The inserter iterators can be used in conjunction with
      the copy() algorithm in a particularly useful way. Here ’ s how you could read values from cin and
      transfer them to a list < T > container:
      list < double > values;
      cout < < "Enter a series of values separated by spaces"
      < < " followed by Ctrl+Z or a letter to end:" < < endl;
      istream_iterator < double > input(cin), input_end;
      copy(input, input_end, back_inserter < list < double > > (values));